Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
172 views
in Technique[技术] by (71.8m points)

SQL Server SP - Pass parameter for "IN" array list?

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)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...