comparing a column to a list of values in t-sql

后端 未结 2 573
感动是毒
感动是毒 2020-12-02 00:27

I have a situation where I am displaying records on a page, and I need a way for the user to select a subset of those records to be displayed on another page. These records

相关标签:
2条回答
  • 2020-12-02 00:56

    this is the best source:

    http://www.sommarskog.se/arrays-in-sql.html

    create a split function, and use it like:

    SELECT
        *
        FROM YourTable  y
        INNER JOIN dbo.splitFunction(@Parameter) s ON y.ID=s.Value
    

    I prefer the number table approach

    For this method to work, you need to do this one time table setup:

    SELECT TOP 10000 IDENTITY(int,1,1) AS Number
        INTO Numbers
        FROM sys.objects s1
        CROSS JOIN sys.objects s2
    ALTER TABLE Numbers ADD CONSTRAINT PK_Numbers PRIMARY KEY CLUSTERED (Number)
    

    Once the Numbers table is set up, create this function:

    CREATE FUNCTION [dbo].[FN_ListToTable]
    (
         @SplitOn  char(1)      --REQUIRED, the character to split the @List string on
        ,@List     varchar(8000)--REQUIRED, the list to split apart
    )
    RETURNS TABLE
    AS
    RETURN 
    (
    
        ----------------
        --SINGLE QUERY-- --this will not return empty rows
        ----------------
        SELECT
            ListValue
            FROM (SELECT
                      LTRIM(RTRIM(SUBSTRING(List2, number+1, CHARINDEX(@SplitOn, List2, number+1)-number - 1))) AS ListValue
                      FROM (
                               SELECT @SplitOn + @List + @SplitOn AS List2
                           ) AS dt
                          INNER JOIN Numbers n ON n.Number < LEN(dt.List2)
                      WHERE SUBSTRING(List2, number, 1) = @SplitOn
                 ) dt2
            WHERE ListValue IS NOT NULL AND ListValue!=''
    
    );
    GO 
    

    You can now easily split a CSV string into a table and join on it:

    select * from dbo.FN_ListToTable(',','1,2,3,,,4,5,6777,,,')
    

    OUTPUT:

    ListValue
    -----------------------
    1
    2
    3
    4
    5
    6777
    
    (6 row(s) affected)
    

    Your can pass in a CSV string into a procedure and process only rows for the given IDs:

    SELECT
        y.*
        FROM YourTable y
            INNER JOIN dbo.FN_ListToTable(',',@GivenCSV) s ON y.ID=s.ListValue
    
    0 讨论(0)
  • 2020-12-02 01:06

    You can use the solution Joel Spolsky recently gave for this problem.

    SELECT * FROM MyTable
    WHERE ',' + 'comma,separated,list,of,words' + ','
      LIKE '%,' + MyTable.word + ',%';
    

    That solution is clever but slow. The better solution is to split the comma-separated string, and construct a dynamic SQL query with the IN() predicate, adding a query parameter placeholder for each element in your list of values:

    SELECT * FROM MyTable
    WHERE word IN ( ?, ?, ?, ?, ?, ?, ?, ? );
    

    The number of placeholders is what you have to determine when you split your comma-separated string. Then pass one value from that list per parameter.

    If you have too many values in the list and making a long IN() predicate is unwieldy, then insert the values to a temporary table, and JOIN against your main table:

    CREATE TEMPORARY TABLE TempTableForSplitValues (word VARCHAR(20));
    
    ...split your comma-separated list and INSERT each value to a separate row...
    
    SELECT * FROM MyTable JOIN TempTableForSplitValues USING (word);
    

    Also see many other similar questions on SO, including:

    • Dynamic SQL Comma Delimited Value Query
    • Passing a varchar full of comma delimited values to a SQL Server IN function
    • Parameterized Queries with Like and In
    • Parameterizing a SQL IN clause?
    0 讨论(0)
提交回复
热议问题