Click to See Complete Forum and Search --> : Parameterized query with WHERE .. IN.. function


ask4help
March 11th, 2007, 12:23 PM
Hi all,

I encountered a problem when selecting using WHERE ... IN ... function.
The sql statement is as below:
"select * from table where id in (@id)"
id column is of integer data type

I passed in a string "1,2,3" into parameter @id, and I get an error saying "Convertion failed when converting nvarchar value '5092, 5093' to data type.

However, when the query is "select * from table where id in (1,2,3)", everything is working fine.

Any idea how to use WHERE ... IN ... function in parameterized sql statement?

Thanks...

Sun_C#
March 12th, 2007, 01:41 AM
See if this helps you.

declare @test varchar(10), @query nvarchar(50)
select @test = '1,2,3'
select @query = N'select * from employee where job_id in (' + @test + ')'
execute sp_executesql @query

ask4help
March 13th, 2007, 09:40 AM
It's working fine with your method. THanks...