9

Is there a way to pass the array of values to the IN section of a SP as a single paramter for SQL Server 2005?

Ex: Select * from MyTable where ID IN(@MyValueArray)

3

1 Answer 1

6

In 2005 and earlier you can't pass an array as a parameter to a stored procedure but to emulate this functionality pass in a comma separated list of IDs as a VARCHAR parameter. You'll then need to parse this list adding each id to a variable table. Then use IN on the result of the table. This is not an elegant solution but it's about the best you can do.

DECLARE @List TABLE (ID INT)

INSERT @List VALUES ('123')
INSERT @List VALUES ('12')

SELECT *
FROM
    MyTable
WHERE
    MyTableID IN (SELECT ID FROM @List)

This is best accomplished by creating a function that takes in a list of ids as a string and returns your list of IDs as a table.

IF EXISTS(
    SELECT *
    FROM sysobjects
    WHERE name = 'ParseIDArray')
BEGIN
    DROP FUNCTION ParseIDArray
END
GO

CREATE FUNCTION [dbo].[ParseIDArray] (@IDList VARCHAR(8000))
RETURNS
    @IDListTable TABLE (ID INT)
AS
BEGIN

    DECLARE
        --@IDList VARCHAR(100),
        @LastCommaPosition INT,
        @NextCommaPosition INT,
        @EndOfStringPosition INT,
        @StartOfStringPosition INT,
        @LengthOfString INT,
        @IDString VARCHAR(100),
        @IDValue INT

    --SET @IDList = '11,12,113'

    SET @LastCommaPosition = 0
    SET @NextCommaPosition = -1

    IF LTRIM(RTRIM(@IDList)) <> ''
    BEGIN

        WHILE(@NextCommaPosition <> 0)
        BEGIN

            SET @NextCommaPosition = CHARINDEX(',',@IDList,@LastCommaPosition + 1)

            IF @NextCommaPosition = 0
                SET @EndOfStringPosition = LEN(@IDList)
            ELSE
                SET @EndOfStringPosition = @NextCommaPosition - 1

            SET @StartOfStringPosition  = @LastCommaPosition + 1
            SET @LengthOfString = (@EndOfStringPosition + 1) - @StartOfStringPosition

            SET @IDString =  SUBSTRING(@IDList,@StartOfStringPosition,@LengthOfString)                  

            IF @IDString <> ''
                INSERT @IDListTable VALUES(@IDString)

            SET @LastCommaPosition = @NextCommaPosition

        END --WHILE(@NextCommaPosition <> 0)

    END --IF LTRIM(RTRIM(@IDList)) <> ''

    RETURN

ErrorBlock:

    RETURN

END --FUNCTION

Here is an example of creating a stored procedure that takes in a list of IDs using this function

IF EXISTS (SELECT * FROM sysobjects WHERE name = 'TestArrayParameter')
BEGIN
    DROP PROCEDURE TestArrayParameter
END
GO



CREATE PROCEDURE TestArrayParameter
    @ArrayParameter VARCHAR(8000)
AS
BEGIN


    SELECT *
    FROM TestTable123
    WHERE TestTblID IN (SELECT ID FROM [dbo].[ParseIDArray](@ArrayParameter))

    -- OR BETTER

    SELECT *
    FROM
        TestTable123 test
        INNER JOIN [dbo].[ParseIDArray](@ArrayParameter) list
         ON list.ID = test.TestTblID

END
GO

CREATE TABLE TestTable123  (TestTblID INT, TestTblVal VARCHAR(50))

INSERT TestTable123 VALUES (3,'Three')
INSERT TestTable123 VALUES (25,'Twenty Five')
INSERT TestTable123 VALUES (100,'One Hundred')

DECLARE @IDList VARCHAR(8000)

SET @IDList = '25,100'

EXEC TestArrayParameter @IDList

DROP TABLE TestTable123
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.