I have a string which contains a comma delimited list of id’s which I want to use in a where clause.
How can I achieve this? Essentially I have:
set @ids = '5,8,14,5324,'
SELECT * FROM theTable WHERE Id IN (@ids)
but @ids is a varchar and Id is an int.
Any ideas much appreciated.
Another option instead of dynamic SQL would be to parse @ids into a #TempTable (one example of a parsing function is here) and then join against it.
or you can bypass the #TempTable completely (the following code assumes the parsing function from the link above)