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)
 How to send multi data in SP to one variable.

Author  Topic 

zubamark
Starting Member

23 Posts

Posted - 2008-01-24 : 10:32:06
Hi,

How to send multi data in one variable.
EXEC ABC '01/20/2008','1,2,3,4'

CREATE PROCEDURE ABC
@Date datetime,
@Status varchar(50)
AS
select * from T1 where Date=@Date and Status in(@Status)

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-01-24 : 10:37:43
http://www.sommarskog.se/arrays-in-sql.html
http://www.sommarskog.se/dynamic_sql.html


E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-01-24 : 10:48:14
Keep in mind that you cant use variable inside in. You need to use dynamic SQL or your statement should be as follows:-

select * from T1 where Date=@Date and @Status like '%' + CAST(Status AS varchar(2)) + '%'

assuming Status is of type int.
Go to Top of Page

Van
Constraint Violating Yak Guru

462 Posts

Posted - 2008-01-24 : 10:54:14
To get good performance, it's a good idea to create a temp table (or table variable, but you can't create an index on a table variable) and parse through the list and put those values in the temp table.

Temp Table:
Status
1
2
3
4

Then you can just join to your temp table in your query.
Go to Top of Page

zubamark
Starting Member

23 Posts

Posted - 2008-01-24 : 11:07:21
How to EXEC ABC '01/20/2008','TempTable'?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-01-24 : 11:10:51
pass values as a string and inside SP get values from string onto temp table. You can even use a user defined function for achieving this. then you may take join with UDF to get results filetered. The UDF can be found out inside these forums. Search for it.Else pass as a string and use LIKE as i suggested.
Go to Top of Page

zubamark
Starting Member

23 Posts

Posted - 2008-01-24 : 12:04:31
Thanks a lot. This link help to solve the problem
http://www.sommarskog.se/arrays-in-sql-2005.html
Go to Top of Page
   

- Advertisement -