1

Possible Duplicate:
Pass a list-structure as an argument to a stored procedure

I am having a session variable that contains multiple comma separated values.I stored these values in an array using Split function.Now I need to pass this array to the stored procedure.Is this possible and if yes how can I use this array in stored procedure?

Community
  • 1
  • 1
Ankur
  • 17
  • 1
  • 3
  • 7

2 Answers2

1

If you are using Sql Server 2008, try passing it as a table value parameter:

Table Value Parameter in SQL Server 2008 and .NET (C#)

Jeff Ogata
  • 56,645
  • 19
  • 114
  • 127
0

I've ran into this exact same scenario before and here is what I did:

Instead of splitting the variable into an array in code I simply passed the entire string of comma delimited text to a stored procedure as an nvarchar. Then I created a database function that takes the value passed in as well as a delimiter and returns a table of the splitted values. See below...

    CREATE FUNCTION [dbo].[split]
    (
        @DataToSplit NVARCHAR(MAX),
        @Delimiter NVARCHAR(5)
    )
    RETURNS @ReturnVal AS TABLE
    (
        ID INT IDENTITY(1, 1),
        Item NVARCHAR(200)
    )
    AS
    BEGIN

        WHILE (CHARINDEX(@DELIMITER, @DataToSplit) > 0)
        BEGIN

            INSERT INTO @ReturnVal
            (
                Item
            )
            VALUES
            (
                SUBSTRING(@DataToSplit, 1, CHARINDEX(@Delimiter, @DataToSplit) - 1)
            )

            SET @DataToSplit = SUBSTRING(@DataToSplit, CHARINDEX(@Delimiter, @DataToSplit) + 1, LEN(@DataToSplit))
        END

        -- Grab the last item of the separated list of items
        INSERT INTO @ReturnVal
        (
            Item
        )
        VALUES
        (
            @DataToSplit
        )

        RETURN

    END

Once you have your split function created you can use it in the stored proc that you are passing the delimited text into and use it in various ways such as...

    CREATE PROCEDURE [dbo].[ExampleProc]
        @Values NVARCHAR(MAX)
    AS

        SET NOCOUNT ON;

        SELECT Item
        FROM [dbo].[split](@Values, ',')

        -- OR

        SELECT Item
        FROM [dbo].[SomeTable]
        WHERE Item IN 
        (
           SELECT Item FROM [dbo].[split](@Values, ',')
        )

        -- OR

        SELECT a.Item
        FROM [dbo].[SomeTable] AS a
        INNER JOIN [dbo].[split](@Values, ',') AS b
            ON a.ID = b.ID
    GO
AdamM
  • 141
  • 5
  • 1
    Possibly the worst way to do it since SQL Server 2005 added good xml handling and SQL Server 2008 added table valued parameters. What if your data has a comma in it...? – gbn Oct 06 '11 at 04:46
  • I would agree that there are better approaches especially table valued parameters, but considering the fact that the Ankur didn't specify which version of SQL Server he was using and the fact that the array would be between 1 - 250 then this is a reasonable and flexible approach. The delimiter can be any character. The comma was used purely as an example which you would have noticed had you read it all. – AdamM Oct 06 '11 at 04:52
  • I didn't read it. xml support was in SQL Server 2000 (just less convenient) so version doesn't apply.. – gbn Oct 06 '11 at 04:56