Click here to Skip to main content
15,913,296 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everybody,
I am using In operator and passing it values through a variable, but it is not returning any value. The In operator using varible's value as a single string.

for example:-

declare @empid varchar(100) =('k','b','c')

select * from table where id in (@empid)

what i should do to solve it.
Thanks for any help

Regards..
dEV kASHYAP
Posted
Comments
Amir Mahfoozi 25-Jan-12 6:44am    
What do you mean by : declare @empid varchar(100) =('k','b','c')
It is an invalid statement.

It seems that you meant :
SQL
declare @empid varchar(100) = 'k,b,c'



So the solution is this :

SQL
declare @empid varchar(100) = 'k,b,c'

declare @xml xml
set @xml = N'<root><r>' + replace(@empid, ',','</r><r>') + '</r></root>'

select * from table where id in
(
select r.value('.','varchar(max)')  as item
from @xml.nodes('//root/r') as records(r)
)


Hope it helps.
 
Share this answer
 
Try select * from table where id in (' + @empid + ')'

Building Dynamic SQL In a Stored Procedure[^] should help you out.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900