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

10,543

Solution 1

This has been asked quite a number of times. SQL Server SP - Pass parameter for “IN” array list?

Also have a look at

Solution 2

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

Solution 3

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.

Share:
10,543
Thomas L Holaday
Author by

Thomas L Holaday

Updated on June 16, 2022

Comments

  • Thomas L Holaday
    Thomas L Holaday about 2 years

    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.