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)
 Dynamic IN

Author  Topic 

kensai
Posting Yak Master

172 Posts

Posted - 2008-06-18 : 03:57:39
I need a procedure accepting series of ID's and retrieve data according to those ID numbers. Like this:

CREATE PROCEDURE tmp
(
@var nvarchar(MAX)
)
AS
select * from Table where ID IN(@var)

I thought of sending ID's like this: '1,2,3'

Of course it doesn't work and gives "can not convert int to nvarchar" error. How can I do this? Dynamic SQL? Or is there a better way?

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-06-18 : 04:04:13
http://www.sommarskog.se/arrays-in-sql-2005.html


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

ranganath
Posting Yak Master

209 Posts

Posted - 2008-06-18 : 04:50:34
hi,

try with this without dynamic

Declare @var varchar(100)
Set @var = '1,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,22,23,24'

select * from
Table where ',' + @var + ',' Like '%,' + cast(Id as varchar(100))+ ',%'
Go to Top of Page
   

- Advertisement -