MonsterBuck
Programmer
I can use the Execute command and the keyword IN() and it works, but when I try and covert the code to sp_executesql I get an error message
Below is just some sample code to set up a test case senerio.
Does anybody know how to use sp_executesql and the keyword IN()? Is this possible?
--******************************************************
create table deleteme (i int)
insert into deleteme
values (1)
insert into deleteme
values (2)
insert into deleteme
values (3)
GO
--this plain execute works
declare @list varchar(50)
set @list = '1,2,3,4'
exec ('select * from deleteme where i in ('+@list+')')
--this works fine with @list only has one value
set @list = '1'
execute sp_executesql
N'select * from deleteme where i in (@listvar)',
N'@listvar varchar(50)',
@listvar = @list
--this doesn't work
--I get the following error message
--Server: Msg 245, Level 16, State 1, Line 1
--Syntax error converting the varchar value '1,2,3,4' to a column of data type int.
set @list = '1,2,3,4'
execute sp_executesql
N'select * from deleteme where i in (@listvar)',
N'@listvar varchar(50)',
@listvar = @list
GO
drop table deleteme
Below is just some sample code to set up a test case senerio.
Does anybody know how to use sp_executesql and the keyword IN()? Is this possible?
--******************************************************
create table deleteme (i int)
insert into deleteme
values (1)
insert into deleteme
values (2)
insert into deleteme
values (3)
GO
--this plain execute works
declare @list varchar(50)
set @list = '1,2,3,4'
exec ('select * from deleteme where i in ('+@list+')')
--this works fine with @list only has one value
set @list = '1'
execute sp_executesql
N'select * from deleteme where i in (@listvar)',
N'@listvar varchar(50)',
@listvar = @list
--this doesn't work
--I get the following error message
--Server: Msg 245, Level 16, State 1, Line 1
--Syntax error converting the varchar value '1,2,3,4' to a column of data type int.
set @list = '1,2,3,4'
execute sp_executesql
N'select * from deleteme where i in (@listvar)',
N'@listvar varchar(50)',
@listvar = @list
GO
drop table deleteme