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)
 Variables with multiple values

Author  Topic 

RyC123
Starting Member

2 Posts

Posted - 2011-10-18 : 05:14:08
New to Forums and need some assistance:

I have the following variable:

DECLARE @X_ID int

select @X_id = id from TABLE where COLUMNNAME = 'VALUE'

Unfortunately there is more than one id for the 'VALUE' in the TABLE. SQL Returns only the last id from the TABLE and not all.

Is there a way to change the script to include all id? Should I rather insert id into a temp table and join this instead of using @X_ID?

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-10-18 : 05:21:23
for that you need a table variable since variable can hold only one value at a time.

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

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-10-18 : 05:28:21
another way is to store values in delimited format in variable

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

Go to Top of Page

RyC123
Starting Member

2 Posts

Posted - 2011-10-18 : 06:12:27
You have tougt me a VERY valuable lesson here. Never heard of table variable but I made the change and it does work. Thanks!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-10-18 : 07:34:14
welcome

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

Go to Top of Page
   

- Advertisement -