Jeremy Stein - Brain

« »

SQL Server T-SQL stored procedure in-clause list from parameter

I tried to do this in a T-SQL stored procedure:

create procedure myproc
@list nvarchar(128)
as
...
select *
from something
where id in (@list)

Which, of course, you can’t do. In the where clause, SQL Server attempts to convert @list into an int, since it appears that I’m checking for id in a list of one int. The solution is to do something like this:

create procedure myproc
@list nvarchar(128)
as
...
select *
from something
where charindex(',' + cast(id as varchar) + ',', ',' + @list + ',') > 0)

I got that from here.

February 14, 2006 No Comments.

No Comments

Be the first to comment!

Leave a Reply

Your email address will not be published. Required fields are marked *

Why ask?

« »