tags:

views:

43

answers:

4

how do I loop through a comma separated variable using tsql in a stored proc

So for instance my list would look like this

"1,2,3,4,5,6,7,8,9,10"

and I would loop thought this list and made some necessary table insert based on this list

A: 

A common problem. Here's just one blog post on the subject: http://bradsruminations.blogspot.com/2010/08/integer-list-splitting-sql-fable.html

Michael J Swart
A: 

You could do it a couple ways, but if this would be a list of ID's it could be done like this as well. It would change your list format a bit.

UPDATE table
SET column = value
WHERE ID in ('1','2','3','4','5','6','7','8','9','10')

You could do a loop as well

DECLARE @List CHAR(100)
DECLARE @ListItem int
DECLARE @Pos int
SET @List = '1,2,3,4,5,6,7,8,9,10'
WHILE LEN(@List) > 0
BEGIN
    --Pull Item Frim List
    SET @Pos = CHARINDEX(',', @List)
    IF @Pos = 0
        BEGIN
            SET @ListItem = @List
        END
    ELSE
        BEGIN
            SET @ListItem = SUBSTRING(@List, 1, @Pos - 1)
        END

    UPDATE table
    SET column = value
    WHERE ID = @ListItem

    --Remove Item Frim List
    IF @Pos = 0
        BEGIN
               SET @List = ''
        END
    ELSE
        BEGIN
                SET @List = SUBSTRING(@List, @Pos + 1, LEN(@List) - @Pos)
        END
END
Dustin Laine
+1  A: 

I'd try to avoid looping and insert the rows directly from your comma list.

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 2005 and Beyond, When Table Value Parameters Do Not Cut it" 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. in general, you need to create a split function. This is how a split function can be used to insert rows:

INSERT INTO YourTableA (colA)
    SELECT
        b.col1
        FROM dbo.yourSplitFunction(@Parameter) b

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))
GO

or insert rows from it:

Create Procedure up_TEST
@Ids VARCHAR(MAX)
,@OtherValue varchar(5)
AS
INSERT INTO YourTableA
        (colA, colB, colC)
    SELECT
        ListValue, @OtherValue, GETDATE()
        FROM dbo.FN_ListToTable(',',@Ids)
GO
KM
A: 

Using CTE (Common Table Expression) is the most elegant solution I think check this question on stackoverflow,

http://stackoverflow.com/questions/314824/t-sql-opposite-to-string-concatenation-how-to-split-string-into-multiple-records

A_Nablsi