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

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 temporary variable

Author  Topic 

KlausEngelInc
Starting Member

8 Posts

Posted - 2009-06-12 : 18:09:29
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

300 Posts

Posted - 2009-06-12 : 18:44:05
[CODE]DECLARE @n1 int
SELECT @n1 = n1*1.2 FROM YourTable
SELECT n1, @n1, @n1 + n1 * 0.8
FROM YourTable[/CODE]


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

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-06-13 : 02:56:25
quote:
Originally posted by Skorch

[CODE]DECLARE @n1 int
SELECT @n1 = n1*1.2 FROM YourTable
SELECT n1, @n1, @n1 + n1 * 0.8
FROM YourTable[/CODE]


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

52326 Posts

Posted - 2009-06-14 : 02:00:33
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

300 Posts

Posted - 2009-06-15 : 11:41:56
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
   

- Advertisement -