views:

42

answers:

3

I have a table named "Documents" containing a column as below:

DocumentID

I have data in the format - @DocID = 1,2,3,4

How do I insert these documentID's in separate rows using a single query?

+1  A: 

If I understand right you want to do this:

INSERT INTO Documents
(DocumentID)
SELECT 1
UNION
SELECT 2
UNION
SELECT 3
UNION
SELECT 4
Sem Dendoncker
I think the OP was looking for a generic solution for every row in the table not just the one row with `1,2,3,4` in it.
KM
Ic, sorry ^^.this was indeed the solution for that specific problem
Sem Dendoncker
+3  A: 

You need a way to split and process the string in TSQL, there are many ways to do this. This article covers the PROs and CONs of just about every method:

Arrays and Lists in SQL Server 2005 and Beyond

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 - Using a Table of Numbers 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 inline_split_me (@SplitOn char(1),@param varchar(7998)) RETURNS TABLE AS
   RETURN(SELECT substring(@SplitOn + @param + ',', Number + 1,
                    charindex(@SplitOn, @SplitOn + @param + @SplitOn, Number + 1) - Number - 1)
                 AS Value
          FROM   Numbers
          WHERE  Number <= len(@SplitOn + @param + @SplitOn) - 1
            AND  substring(@SplitOn + @param + @SplitOn, Number, 1) = @SplitOn)

GO 

You can now easily split a CSV string into a table and join on it:

select * from dbo.inline_split_me(';','1;22;333;4444;;') where LEN(Value)>0

OUTPUT:

Value
----------------------
1
22
333
4444

(4 row(s) affected)

to make you new table use this:

--set up tables:
DECLARE @Documents table (DocumentID varchar(500), SomeValue varchar(5))
INSERT @Documents VALUES ('1,2,3,4','AAA')
INSERT @Documents VALUES ('5,6'    ,'BBBB')

DECLARE @NewDocuments table (DocumentID int, SomeValue varchar(5))

--populate NewDocuments
INSERT @NewDocuments
    (DocumentID, SomeValue)
SELECT
    c.value,a.SomeValue
    FROM @Documents    a
        CROSS APPLY dbo.inline_split_me(',',a.DocumentID) c

 --show NewDocuments contents:
select * from @NewDocuments

OUTPUT:

DocumentID  SomeValue
----------- ---------
1           AAA
2           AAA
3           AAA
4           AAA
5           BBBB
6           BBBB

(6 row(s) affected)

if you don't want to create a Numbers tableand are running SQL Server 2005 and up, you can just use this split function (no Numbers table required):

CREATE FUNCTION inline_split_me (@SplitOn char(1),@String varchar(7998))
RETURNS TABLE AS
RETURN (WITH SplitSting AS
           (SELECT
                LEFT(@String,CHARINDEX(@SplitOn,@String)-1) AS Part
                    ,RIGHT(@String,LEN(@String)-CHARINDEX(@SplitOn,@String)) AS Remainder
                WHERE @String IS NOT NULL AND CHARINDEX(@SplitOn,@String)>0
            UNION ALL
            SELECT
                LEFT(Remainder,CHARINDEX(@SplitOn,Remainder)-1)
                    ,RIGHT(Remainder,LEN(Remainder)-CHARINDEX(@SplitOn,Remainder))
                FROM SplitSting
                WHERE Remainder IS NOT NULL AND CHARINDEX(@SplitOn,Remainder)>0
            UNION ALL
            SELECT
                Remainder,null
                FROM SplitSting
                WHERE Remainder IS NOT NULL AND CHARINDEX(@SplitOn,Remainder)=0
           )
           SELECT Part FROM SplitSting
       )
GO
KM
+1 for Sommarskog
Philip Kelley
+1  A: 

+1 for KM's thorough explanation. This will get the job done quickly but maybe not necessarily most efficiently (again see KM's response for all the options)

My quick response:

Install SQL# (it's free and very useful)

Then

INSERT INTO Documents (documentId)
SELECT SplitVal FROM SQL#.String_Split(@DocId, ',', 1) 
Joel Mansford
@Joel Mansford, no offense intended, but I would be very leery of installing something like this on a production SQL Server, who knows what payload might be hidden inside that. The code in your answer runs on a single variable's value and not on every row in a table. As a result, you'd need the `CROSS APPLY` like in my answer to run the function on all rows in the old table.
KM
@KM in the example given the input was held within a variable @DocId. Not sure if this was an oversight by OP but it does make sense that the input isn't in multiple rows
Joel Mansford