Please start any new threads on our new site at http://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 temporary variable
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

KlausEngelInc
Starting Member

8 Posts

Posted - 06/12/2009 :  18:09:29  Show Profile  Reply with Quote
I'm trying to compute several values on the fly. How can I create a temporary variable to use in my query?

SELECT n1, (n1*1.2), (n1*1.2) + (n1*0.8)
FROM TABLE

I would like to establish the computed value of (n1*1.2) as a variable so I can continue to use it through the rest of my query. ANy help is appreciated.

Skorch
Constraint Violating Yak Guru

USA
300 Posts

Posted - 06/12/2009 :  18:44:05  Show Profile  Reply with Quote
DECLARE @n1 int
SELECT @n1 = n1*1.2 FROM YourTable
SELECT n1, @n1, @n1 + n1 * 0.8
FROM YourTable



Some days you're the dog, and some days you're the fire hydrant.
Go to Top of Page

madhivanan
Premature Yak Congratulator

India
22864 Posts

Posted - 06/13/2009 :  02:56:25  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
quote:
Originally posted by Skorch

DECLARE @n1 int
SELECT @n1 = n1*1.2 FROM YourTable
SELECT n1, @n1, @n1 + n1 * 0.8
FROM YourTable



Some days you're the dog, and some days you're the fire hydrant.

Reliable only if it has single row


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52326 Posts

Posted - 06/14/2009 :  02:00:33  Show Profile  Reply with Quote
quote:
Originally posted by KlausEngelInc

I'm trying to compute several values on the fly. How can I create a temporary variable to use in my query?

SELECT n1, (n1*1.2), (n1*1.2) + (n1*0.8)
FROM TABLE

I would like to establish the computed value of (n1*1.2) as a variable so I can continue to use it through the rest of my query. ANy help is appreciated.



i think what you need is a table variable to store all these computed values and use it later as you need to do it for each row of your table.
Go to Top of Page

Skorch
Constraint Violating Yak Guru

USA
300 Posts

Posted - 06/15/2009 :  11:41:56  Show Profile  Reply with Quote
quote:
Reliable only if it has single row

Madhivanan

Failing to plan is Planning to fail



DOH! Shows how much I was ready to leave on Friday

Some days you're the dog, and some days you're the fire hydrant.
Go to Top of Page
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000