Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does SQL have a list type that can be used in a WHERE ... IN clause? [duplicate]

Tags:

sql

tsql

Possible Duplicates:
Parameterizing a SQL IN clause?
SQL Server SP - Pass parameter for “IN” array list?

I need to search for a haphazard set of integers on two different tables:

SELECT 
  col_1, col_2 
FROM 
  LIKES_NUMBERS 
WHERE 
  col_1 IN (1,2,3,5,7,1021,10041411)

SELECT 
  col_one, col_two 
FROM 
  LIKES_NAMES 
WHERE 
  col_one IN (1,2,3,5,7,1021,10041411)

Is there a SQL list type that can be passed to IN so that I don't repeat myself? E.G.

DECLARE @stuff UNOBTAINIUM(1,2,3,5,7,1021,10041411)
-- ...
WHERE col_1 IN (@stuff)
-- ...
WHERE col_one IN (@stuff)

Creating a temporary table comes to mind, but that seems brutal.

like image 980
Thomas L Holaday Avatar asked Jan 15 '10 15:01

Thomas L Holaday


1 Answers

Yes, you can use a table variable for this. It's like a temp table, but locally scoped.

like image 62
Craig Stuntz Avatar answered Oct 13 '22 19:10

Craig Stuntz