Is it possible to use a parameter in conjuntion with an IN statement?
For example, if I have the following SQL,
select * from table where key in ('1','2','3')
How can I make the values in the IN be a param? If I do the following.
select * from table where key in (@invals)
I can't make a parameter like so,
param = new sqlparameter("@invals","'1','2','3'")
It simply returns no results.
Is there a way to make a parameter that will work?
Thanks
Marina - 03 Jan 2006 20:24 GMT
You can't, there is no way to do this as far as i know. You have to
concatenate the strings together for the query.
> Is it possible to use a parameter in conjuntion with an IN statement?
>
[quoted text clipped - 11 lines]
>
> Thanks
Jon - 03 Jan 2006 20:50 GMT
Darn, that's what I figured. Thanks
> You can't, there is no way to do this as far as i know. You have to
> concatenate the strings together for the query.
[quoted text clipped - 14 lines]
>>
>> Thanks
W.G. Ryan - MVP - 03 Jan 2006 21:13 GMT
http://support.microsoft.com/default.aspx?scid=kb;en-us;555167
> Is it possible to use a parameter in conjuntion with an IN statement?
>
[quoted text clipped - 11 lines]
>
> Thanks
William (Bill) Vaughn - 04 Jan 2006 00:40 GMT
Yes, this is an interesting approach and I have another I talk about in my
book that uses a Table-type Function to parse the delimited string...

Signature
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
INETA Speaker
www.betav.com/blog/billva
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
> http://support.microsoft.com/default.aspx?scid=kb;en-us;555167
>> Is it possible to use a parameter in conjuntion with an IN statement?
[quoted text clipped - 12 lines]
>>
>> Thanks
Jon - 04 Jan 2006 16:12 GMT
Thanks. This would work well in stored procedures, but unfortunately I am
using regular sql sentences.
> http://support.microsoft.com/default.aspx?scid=kb;en-us;555167
>> Is it possible to use a parameter in conjuntion with an IN statement?
[quoted text clipped - 12 lines]
>>
>> Thanks
Fox - 14 Jan 2006 12:19 GMT
Hi
the solution is next ->
DECLARE @smallSQL
SET @smallSQL = "SELECT * FROM Main WHERE Price IN "+@Params
Exec (@smallSQL)
========
Where @Params can be input value of proc like (1,2,3)
:-)
good luck
> Is it possible to use a parameter in conjuntion with an IN statement?
>
[quoted text clipped - 11 lines]
>
> Thanks
W.G. Ryan eMVP - 15 Jan 2006 03:22 GMT
Another possible approach is to do the following:
http://support.microsoft.com/default.aspx?scid=kb;en-us;555167 . The guy
that wrote the article is a real genius so I figured I'd take this
opportunity to plug his article ;-)
> Hi
>
[quoted text clipped - 25 lines]
>>
>> Thanks