Please start any new threads on our new site at http://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 How to pass variable to the IN clause
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

timlisten
Starting Member

USA
26 Posts

Posted - 06/11/2012 :  18:30:28  Show Profile  Reply with Quote
Hi, I have a query where I need to use the IN variable. Is there a way to do it without using dynamic query? For example,
select *
from table
where data IN ('A', 'B')

works fine. Is there a way for me to pass a variable to the IN clause instead of the static 'A', 'B'.

visakh16
Very Important crosS Applying yaK Herder

India
52326 Posts

Posted - 06/11/2012 :  18:32:43  Show Profile  Reply with Quote
you can see



DECLARE @VarList varchar(1000)

SET @VarList = 'A,B'

SELECT *
FROM table
WHERE ',' + @VarList + ',' LIKE '%,' + data + ',%'


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

timlisten
Starting Member

USA
26 Posts

Posted - 06/11/2012 :  21:44:11  Show Profile  Reply with Quote
that works great, thank you so much visakh16. I just wished I had come to this forum years ago.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52326 Posts

Posted - 06/12/2012 :  15:19:33  Show Profile  Reply with Quote
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Sachin.Nand
Flowing Fount of Yak Knowledge

2937 Posts

Posted - 06/12/2012 :  15:48:19  Show Profile  Reply with Quote

DECLARE @VarList varchar(1000)

SET @VarList = 'A,B'

SELECT *
FROM table
WHERE data IN(select @VarList)


After Monday and Tuesday even the calendar says W T F ....
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52326 Posts

Posted - 06/12/2012 :  20:19:35  Show Profile  Reply with Quote
quote:
Originally posted by Sachin.Nand


DECLARE @VarList varchar(1000)

SET @VarList = 'A,B'

SELECT *
FROM table
WHERE data IN(select @VarList)


After Monday and Tuesday even the calendar says W T F ....


sorry didnt understand how this will work.

check below code


DECLARE @VarList varchar(1000)

declare @t table
(
id int identity(1,1),
chr char(1)
)
insert @t (chr)
values ('A'),('C'),('D'),('F')

SET @VarList = 'A,F'

SELECT *
FROM @t
WHERE chr IN(select @VarList)



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Sachin.Nand
Flowing Fount of Yak Knowledge

2937 Posts

Posted - 06/13/2012 :  02:47:56  Show Profile  Reply with Quote
quote:
Originally posted by visakh16

quote:
Originally posted by Sachin.Nand


DECLARE @VarList varchar(1000)

SET @VarList = 'A,B'

SELECT *
FROM table
WHERE data IN(select @VarList)


After Monday and Tuesday even the calendar says W T F ....


sorry didnt understand how this will work.

check below code


DECLARE @VarList varchar(1000)

declare @t table
(
id int identity(1,1),
chr char(1)
)
insert @t (chr)
values ('A'),('C'),('D'),('F')

SET @VarList = 'A,F'

SELECT *
FROM @t
WHERE chr IN(select @VarList)



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/





Naah..Just made a wild guess..

Dont take it to seriously..

After Monday and Tuesday even the calendar says W T F ....
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52326 Posts

Posted - 06/13/2012 :  10:50:46  Show Profile  Reply with Quote
np

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

namman
Constraint Violating Yak Guru

USA
285 Posts

Posted - 06/14/2012 :  00:13:21  Show Profile  Reply with Quote
If your table is large, use function for better performance.
Go to Top of Page
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.07 seconds. Powered By: Snitz Forums 2000