This works:
declare @ids varchar(50) = '1, 2, 3'
create table #test (id int)
exec('insert into #test select userid from users where userid in(' + @ids + ')')
This doesn't:
declare @ids varchar(50) = '1, 2, 3'
create table #test (id int)
insert into #test select userid from users where userid in(@ids)
It (kinda obviously) returns:
Conversion failed when converting the varchar value '1, 2, 3' to data type int.
Is there an elegant way to make the query work without using exec()
, like I try to do in the second piece of code?