String separated by commas in columns by using SQL Server 2008 R2

advertisements

I want to insert the bulk values into the table by using stored procedure.

Here is the following table called m1 with two fields:

create table m1
(
    cola varchar(50),
    colb varchar(50)
);

Procedure m1_test to insert the values into the table:

create procedure m1_test
    @stringCola varchar(max),
    @stringColb varchar(max)

AS

    INSERT INTO m1 values(@stringCola,@stringColb);

GO

The above procedure is fine for inserting single values as shown below:

---EXECUTE SP
EXECUTE m1_test @stringCola = 'A1',@stringColb = 'Z1';

But If I want to insert the more values at a time in single string like shown below:

---EXECUTE SP
EXECUTE m1_test @stringCola = 'A1,A2,A3',@stringColb = 'Z1,Z2,Z3';

The output should be:

 cola   colb
 ------------
 A1     Z1
 A2     Z2
 A3     Z3


First you need to create a table valued function which will return the comma separated values in the form of a table.

Implement the below code for the function, which I found here, with a slight modification to return an identity column based on which you will later do a JOIN to get a tuple of the 1st value for column A and the first value for column B (A1, Z1) and so on the 2nd, 3rd etc. :

CREATE FUNCTION Split (@InputString VARCHAR(8000), @Delimiter VARCHAR(50))

RETURNS @Items TABLE (ID INTEGER IDENTITY(1,1), Item VARCHAR(8000))

AS
BEGIN
      IF @Delimiter = ' '
      BEGIN
            SET @Delimiter = ','
            SET @InputString = REPLACE(@InputString, ' ', @Delimiter)
      END

      IF (@Delimiter IS NULL OR @Delimiter = '')
            SET @Delimiter = ','

--INSERT INTO @Items VALUES (@Delimiter) -- Diagnostic
--INSERT INTO @Items VALUES (@InputString) -- Diagnostic

      DECLARE @Item VARCHAR(8000)
      DECLARE @ItemList VARCHAR(8000)
      DECLARE @DelimIndex INT

      SET @ItemList = @InputString
      SET @DelimIndex = CHARINDEX(@Delimiter, @ItemList, 0)
      WHILE (@DelimIndex != 0)
      BEGIN
            SET @Item = SUBSTRING(@ItemList, 0, @DelimIndex)
            INSERT INTO @Items VALUES (@Item)

            -- Set @ItemList = @ItemList minus one less item
            SET @ItemList = SUBSTRING(@ItemList, @DelimIndex+1, LEN(@ItemList)-@DelimIndex)
            SET @DelimIndex = CHARINDEX(@Delimiter, @ItemList, 0)
      END -- End WHILE

      IF @Item IS NOT NULL -- At least one delimiter was encountered in @InputString
      BEGIN
            SET @Item = @ItemList
            INSERT INTO @Items VALUES (@Item)
      END

      -- No delimiters were encountered in @InputString, so just return @InputString
      ELSE INSERT INTO @Items VALUES (@InputString)

      RETURN

END -- End Function
GO

---- Set Permissions
--GRANT SELECT ON Split TO UserRole1
--GRANT SELECT ON Split TO UserRole2
--GO

Now, after creating this function, modify your stored procedure to:

CREATE PROCEDURE m1_test @stringCola VARCHAR(max), @stringColb VARCHAR(max)
AS
INSERT INTO m1
SELECT A.Item, B.Item
FROM Split(@stringColA, ',') A
    INNER JOIN Split(@stringColB, ',') B ON A.ID = B.ID
GO