SQL - Stored Procedure with Select Statement using IN (@Variable_CommaDelimitedListOfIDS)

前端 未结 7 2238
无人及你
无人及你 2020-12-17 02:42

I am creating a stored procedure to which I want to pass as variable a comma delimited list of Ids. I want to use the Ids into a select statement, something like:



        
相关标签:
7条回答
  • 2020-12-17 02:51

    Since you're using SQL Server 2008, have a look at table-valued parameters.

    0 讨论(0)
  • 2020-12-17 02:56
    CREATE PROCEDURE PROCEDURENAME
    @Id Numeric(18,0)
    AS
    BEGIN
        SELECT * FROM tableName
        WHERE tableName.Id IN (@Id)
    END
    
    0 讨论(0)
  • 2020-12-17 02:58

    If you're having this problem, you should read Sommarskog's articles.

    http://www.sommarskog.se/index.html

    I recommend:

    • Arrays and Lists in Sql Server
    • The curse and blessings of dynamic SQL.
    0 讨论(0)
  • 2020-12-17 03:10

    For those cases I use this table function, which you can adapt to your needs:

    CREATE FUNCTION dbo.f_params_to_list (@par VARCHAR(500))
    returns @result TABLE (value VARCHAR(30))
    AS  
    begin
         DECLARE @TempList table
              (
              value VARCHAR(30)
              )
    
         DECLARE @Value varchar(30), @Pos int
    
         SET @par = LTRIM(RTRIM(@par))+ ','
         SET @Pos = CHARINDEX(',', @par, 1)
    
         IF REPLACE(@par, ',', '') <> ''
         BEGIN
              WHILE @Pos > 0
              BEGIN
                   SET @Value = LTRIM(RTRIM(LEFT(@par, @Pos - 1)))
                   IF @Value <> ''
                   BEGIN
                        INSERT INTO @TempList (value) VALUES (@Value) --Use Appropriate conversion
                   END
                   SET @par = RIGHT(@par, LEN(@par) - @Pos)
                   SET @Pos = CHARINDEX(',', @par, 1)
    
              END
         END    
         INSERT @result
         SELECT value 
            FROM @TempList
         RETURN
    END    
    

    In your stored procedure you would use it like this:

    Create Procedure up_TEST
    @Ids VARCHAR(MAX)
    AS
    SELECT * FROM ATable a
    WHERE a.Id IN(SELECT value FROM dbo.f_params_to_list(@Ids))
    
    0 讨论(0)
  • 2020-12-17 03:10

    One way could be split and insert the ids into a tempory table and than use SQL IN clause...

    0 讨论(0)
  • 2020-12-17 03:11

    Use a table values parameter (new in SQl Server 2008). Set it up by creating the actual table parameter type:

    CREATE TYPE IntTableType AS TABLE (ID INTEGER PRIMARY KEY)
    

    Your procedure would then be:

    Create Procedure up_TEST
        @Ids IntTableType READONLY
    AS
    
    SELECT * 
        FROM ATable a
        WHERE a.Id IN (SELECT ID FROM @Ids)
    
    RETURN 0
    GO
    

    if you can't use table value parameters, see: "Arrays and Lists in SQL Server 2008 Using Table-Valued Parameters" by Erland Sommarskog , then there are many ways to split string in SQL Server. This article covers the PROs and CONs of just about every method:

    "Arrays and Lists in SQL Server 2005 and Beyond, When Table Value Parameters Do Not Cut it" by Erland Sommarskog

    You need to create a split function. This is how a split function can be used:

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

    I prefer the number table approach to split a string in TSQL but there are numerous ways to split strings in SQL Server, see the previous link, which explains the PROs and CONs of each.

    For the Numbers Table method to work, you need to do this one time table setup, which will create a table Numbers that contains rows from 1 to 10,000:

    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 split 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:

    Create Procedure up_TEST
    @Ids VARCHAR(MAX)
    AS
    SELECT * FROM ATable a
    WHERE a.Id IN (SELECT ListValue FROM dbo.FN_ListToTable(',',@Ids))
    
    0 讨论(0)
提交回复
热议问题