Home All Groups Group Topic Archive Search About
Author
3 Jan 2006 8:19 PM
Jon
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

Author
3 Jan 2006 8:24 PM
Marina
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.

Show quote
"Jon" <ruffl***@msn.com> wrote in message
news:%23MmjmMKEGHA.3892@TK2MSFTNGP10.phx.gbl...
> 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
>
Author
3 Jan 2006 8:50 PM
Jon
Darn, that's what I figured.  Thanks


Show quote
"Marina" <someone@nospam.com> wrote in message
news:OUEooOKEGHA.3404@TK2MSFTNGP09.phx.gbl...
> 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.
>
> "Jon" <ruffl***@msn.com> wrote in message
> news:%23MmjmMKEGHA.3892@TK2MSFTNGP10.phx.gbl...
>> 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
>>
>
>
Author
3 Jan 2006 9:13 PM
W.G. Ryan - MVP
http://support.microsoft.com/default.aspx?scid=kb;en-us;555167
Show quote
"Jon" <ruffl***@msn.com> wrote in message
news:%23MmjmMKEGHA.3892@TK2MSFTNGP10.phx.gbl...
> 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
>
Author
4 Jan 2006 12:40 AM
William (Bill) Vaughn
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...

--
____________________________________
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.
__________________________________

Show quote
"W.G. Ryan - MVP" <WilliamRyan@nospam.gmail.com> wrote in message
news:Oc9zAqKEGHA.2036@TK2MSFTNGP14.phx.gbl...
> http://support.microsoft.com/default.aspx?scid=kb;en-us;555167
> "Jon" <ruffl***@msn.com> wrote in message
> news:%23MmjmMKEGHA.3892@TK2MSFTNGP10.phx.gbl...
>> 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
>>
>
>
Author
4 Jan 2006 4:12 PM
Jon
Thanks.  This would work well in stored procedures, but unfortunately I am
using regular sql sentences.


Show quote
"W.G. Ryan - MVP" <WilliamRyan@nospam.gmail.com> wrote in message
news:Oc9zAqKEGHA.2036@TK2MSFTNGP14.phx.gbl...
> http://support.microsoft.com/default.aspx?scid=kb;en-us;555167
> "Jon" <ruffl***@msn.com> wrote in message
> news:%23MmjmMKEGHA.3892@TK2MSFTNGP10.phx.gbl...
>> 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
>>
>
>
Author
14 Jan 2006 12:19 PM
Fox
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

Show quote
"Jon" <ruffl***@msn.com> wrote in message
news:%23MmjmMKEGHA.3892@TK2MSFTNGP10.phx.gbl...
> 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
>
Author
15 Jan 2006 3:22 AM
W.G. Ryan eMVP
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 ;-)
Show quote
"Fox" <m***@job.am> wrote in message
news:ORSI9SQGGHA.3984@TK2MSFTNGP14.phx.gbl...
> 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
>
> "Jon" <ruffl***@msn.com> wrote in message
> news:%23MmjmMKEGHA.3892@TK2MSFTNGP10.phx.gbl...
>> 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
>>
>
>

AddThis Social Bookmark Button