If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Microsoft SQL Server > SQL Syntax Help

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-30-09, 14:21
TML TML is offline
Registered User
 
Join Date: Jan 2009
Posts: 2
SQL Syntax Help

Is it possible to pass a comma delimited list of integers as a parameter into an IN function like this?

SELECT *
FROM MyTable
WHERE MyID IN (@MyVar)

'MyID' is an INT datatype and I'd like '@MyVar' to equal 1,2,3.

Many thanks in advance.

TML
Reply With Quote
  #2 (permalink)  
Old 01-30-09, 14:24
Thrasymachus Thrasymachus is offline
SQL Server Street Fighter
 
Join Date: Nov 2004
Location: Down The Rabbit Hole
Posts: 7,979
I generally feed my CSVs into a table with something like this....

Simple SQL CSV to Table Code Bank

and then I join to the table.
__________________
software development is where smart people go to waste their lives
Reply With Quote
  #3 (permalink)  
Old 01-31-09, 00:09
bklr bklr is offline
Registered User
 
Join Date: Dec 2008
Posts: 133
try like this
declare @MyVar varchar(32)
set @MyVar = '1,5,7'

SELECT *
FROM mytable
WHERE '%,'+ @MyVar+',%' like '%,'+ convert(varchar(32),myid)+',%'
Reply With Quote
  #4 (permalink)  
Old 01-31-09, 14:58
TML TML is offline
Registered User
 
Join Date: Jan 2009
Posts: 2
That's perfect!

Thanks for your help.

TML
Reply With Quote
  #5 (permalink)  
Old 02-01-09, 02:05
bklr bklr is offline
Registered User
 
Join Date: Dec 2008
Posts: 133
welcome yaar
Reply With Quote
  #6 (permalink)  
Old 02-01-09, 09:50
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,602
Be aware that the LIKE solution doesn't scale well. It guarantees a table scan, so on large tables it will be slow.

The solution that Thrasymachus proposed works against a column value instead of an expression, so it can use indicies. It will scale well against the largest tables you can create using SQL Server.

-PatP
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On