Possible Duplicates:
T-SQL stored procedure that accepts multiple Id values
Parameterizing a SQL IN clause?
I have a stored procedure who have to select values with a filter system.
Imagine a Table
ColA | ColB | ColC |
'A' 12 1
'B' 13 2
'C' 14 3
I need to give to stored procedure a list of ColC values for those a want the values, example (1,3)
will give me ‘A’ and ‘C’.
I know it is possible to do a IN statement like :
Select *
from Table
where ColC in (1,3)
But how make this dynamic? a solution will be to construct the sql command in a Varchar variable like
SET @MyList ='(1,3)'
SET @SQL = '
Select *
from Table
where ColC in ' + @MyList
And then execute @SQL
But is it the best way? To make dynamic command like this is not too heavy?
Thanks for advices.
There are plenty of different options on how to pass arrays to stored procedures, however, the common practice is building the query like you have shown, hence I think it’s the best solution here.
EDIT: Arrays in SQL 2008 still shows building a query as the first approach.