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)
 assgining variables to multiple columns

Author  Topic 

mary_itohan
Posting Yak Master

191 Posts

Posted - 2008-09-13 : 16:53:32
Hello,
I will like to know whats the most efficient way of achieving this ?

I have a table with multiple columns i want to assign to variables.

1. Do I run a select command each time i want to assign each column to a variable ???

2. Or is there a better and more efficient way or achieving this ?

declare @a varchar(20),@b varchar(20),@c varchar(20)

set @a = select columnA from testtable where user = 'test'
set @b = select columnB from testtable where user = 'test'
set @c = select columnC from testtable where user = 'test'


Hope this is clear ?
thanks







_____________________


Yes O !

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-09-13 : 17:31:52
select @a = columnA, @b = columnB, @c = columnC
from testtable
where user = 'test'


Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

mary_itohan
Posting Yak Master

191 Posts

Posted - 2008-09-13 : 17:46:20
thanks Tkizer

_____________________


Yes O !
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-09-14 : 09:24:26
are you sure you'll have a single value returned for each column from table (single record existing for user = 'test'). if not what you need is to oput it to temporary table.variables can hold only one value at a time so if you've multiple values coming variable will have only a single value.
Go to Top of Page
   

- Advertisement -