Is there a way to create a query that can be like:
DECLARE @.group_id_list varchar(100)
SET @.group_id_list='100,101,150'
SELECT * FROM abc WHERE abc_id IN (@.group_id_list)
I get the error "Syntax error converting the varchar value '100,101,150' to a column of data type int.
Do I need to resort to a dynamic SQL statement?
You do.|||Not if you don't want to. In many cases I prefer to use a udf that I've created that takes a comma-delimited varchar and returns a table. Then you can either join on the table to limit your results, or you can use IN (SELECT id FROM Split(@.param,DEFAULT) alias1) in your where clause. The second param into my Split UDF is what the separator is (default is comma).|||Thank you. I actually ended up doing something similar (never thought of using a UDF):
DECLARE @.groups TABLE (group_id int)
I ran a while loop inserting the values into the @.groups table, then used:
SELECT * FROM abc WHERE abc_id IN (SELECT group_id FROM @.groups)
I don't know if it's efficient, but my dynamic SQL statement was going to exceed 14k in length!
No comments:
Post a Comment