Does SQL have a list type that can be used in a WHERE ... IN clause? [duplicate]
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.
Yes, you can use a table variable for this. It's like a temp table, but locally scoped.
This has been asked quite a number of times. SQL Server SP - Pass parameter for “IN” array list?
Also have a look at
- Arrays and Lists in SQL Server
- Arrays and Lists in SQL Server 2005 and Beyond
There's no list type. Temporary table should be fine or you can use a table variable.
When using MS-SQL, I tend to use XML documents for this purpose, which can be easily passed from non-SQL clients (unlike temp tables, I believe) and queried by MS-SQL's T-SQL syntax.
精彩评论