set @tmp= Selct … | SQL Server Performance Forums

SQL Server Performance Forum – Threads Archive

set @tmp= Selct …

Hi, it must by very simple… How can I store some result of select ( for example
Set @tmp = Select sum(field1) from Table1
to some variable ? Thanks, Lubo
declare @tmp int
Select @tmp = sum(field1) from Table1
This one should also work – look at the brackets: Declare @tmp INT Set @tmp = (Select sum(field1) from Table1)
Thanks, Lubo
quote:Originally posted by Adriaan This one should also work – look at the brackets: Declare @tmp INT Set @tmp = (Select sum(field1) from Table1)
It should be noted that this type of assignment will work only if the select returns only one value Madhivanan Failing to plan is Planning to fail
Yes , I found it. I need make with this statement in the cycle. Nevermind , i will use cursor for this summing.
Lubo
quote:Originally posted by Madhivanan
quote:Originally posted by Adriaan This one should also work – look at the brackets: Declare @tmp INT Set @tmp = (Select sum(field1) from Table1)
It should be noted that this type of assignment will work only if the select returns only one value Madhivanan Failing to plan is Planning to fail
True. But the other format also has a problem: it would indeed not run into error if the query returns multiple rows, but in that case only the value from the last row is passed to the variable. And obviously the "last" row can be totally arbitrary.
>>it would indeed not run into error if the query returns multiple rows, but in that case only the value from the last row is passed to the variable. And obviously the "last" row can be totally arbitrary. Yes. >>I need make with this statement in the cycle. Nevermind , i will use cursor for this summing. Why do you want to use Cursor?
Can you post your exact requirement?
Madhivanan Failing to plan is Planning to fail

DECLARE @priceModif as decimal, @ns as varchar(20),
drop table sum_zl_Objem
CREATE TABLE sum_to_Price_Modif
(
id int identity,
program varchar(50) NULL,
ns varchar(50) NULL,
price decimal(18, 0) NULL,
priceModif decimal(18, 0) NULL
) ON [PRIMARY]
Insert sum_to_Price_Modif (ns,priceModif )
..
..
group by l.ns this moment my values are store to table sum_to_Price_Modif Then a will read table sum_to_Price_Modif row by row in the cursor and to do something with ever record
Declare au_cursor Cursor FOR Select ns,priceModif from sum_to_Price_Modif Open au_cursor
Fetch Next From au_cursor into @ns, @priceModif WHILE (@@FETCH_STATUS = 0) .
.
.
BEGIN
End Fetch Next From au_cursor into @ns
END Close det_cursor Deallocate det_cursor
Result during run of cyclu are stored to table sum_to_Price_Modif Lubo
there were a mistake on the of code .i know about it. It happend when i wrote last message to forum.I took a part of code , i make with cursor det and au.
It would be helpful if you post some sample data and the result you want Madhivanan Failing to plan is Planning to fail
Thanks you very much, but it’s difficult . There are more tables , some procedures too in cycle. I try it solve. When it will be necessary i send you it.
Lubo
]]>