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.
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 TABLEI 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 intSELECT @n1 = n1*1.2 FROM YourTableSELECT n1, @n1, @n1 + n1 * 0.8FROM YourTable[/CODE]Some days you're the dog, and some days you're the fire hydrant. |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-06-13 : 02:56:25
|
quote: Originally posted by Skorch [CODE]DECLARE @n1 intSELECT @n1 = n1*1.2 FROM YourTableSELECT n1, @n1, @n1 + n1 * 0.8FROM YourTable[/CODE]Some days you're the dog, and some days you're the fire hydrant.
Reliable only if it has single rowMadhivananFailing to plan is Planning to fail |
|
|
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 TABLEI 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. |
|
|
Skorch
Constraint Violating Yak Guru
300 Posts |
Posted - 2009-06-15 : 11:41:56
|
quote: Reliable only if it has single rowMadhivananFailing to plan is Planning to fail
DOH! Shows how much I was ready to leave on FridaySome days you're the dog, and some days you're the fire hydrant. |
|
|
|
|
|