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
 General SQL Server Forums
 New to SQL Server Programming
 parse SELECT result to more than one variable

Author  Topic 

xhostx
Constraint Violating Yak Guru

277 Posts

Posted - 2012-07-19 : 11:16:04
Hi,

Is it possible to store result of a select of the same column in more than one variable:

This is what result is:
AAAA
BBBB
CCCC

I would like to store the 3 different values into different variables.

Is it possible without using cursor?

Thanks,

--------------------------
Get rich or die trying
--------------------------

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2012-07-19 : 11:19:25
dejavu
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=176927


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-07-19 : 11:33:30
first explain us whats the need for this requirement

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2012-07-19 : 11:36:48
Here is one way:
DECLARE 
@A VARCHAR(50),
@B VARCHAR(50),
@C VARCHAR(50)

DECLARE @Foo TABLE (Val VARCHAR(50))

INSERT @Foo
VALUES
('AAAA'),
('BBBB'),
('CCCC')


SELECT @A = [AAAA], @B = [BBBB], @C = [CCCC]
FROM
(
SELECT Val FROM @Foo
) AS Source
PIVOT
(
MAX(Val)
FOR Val IN ([AAAA], [BBBB], [CCCC])
) AS Piv


SELECT @A, @B, @C
Go to Top of Page
   

- Advertisement -