Click here to Skip to main content
16,004,647 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi,

I have a table 4 columns, I need to do an insert using spring split function twice
but i'm getting an error on the second string split

What I have tried:

insert into Table1 (Id, Field1, First, Second )
select @Id, 0, value from STRING_SPLIT(@First, ','), value from STRING_SPLIT(@Second, ',')


Example for what is needed, if @Id = 1 and @First = 1,2,3 @Second = 4,5,6
then the table will contain the following

Id Field1 First  Second
1   0      1      4
1   0      2      5
1   0      3      6
Posted
Updated 12-Aug-21 7:43am
v4

1 solution

You haven't told us what error you're getting. But in this case, it's fairly obvious: that's not a valid syntax for the INSERT or SELECT statements.

STRING_SPLIT returns a table with multiple rows, one for each value in the split string. SQL has no idea that you want to take the first two values from each and transpose them into columns to insert into your table.

You can't use STRING_SPLIT for this.
SQL
INSERT INTO Table1 (Id, Field1, Field2, Field3, Field4)
SELECT
    @Id,
    Left(@First, CharIndex(',', @First) - 1),
    Substring(@First, CharIndex(',', @First) + 1, Len(@First)),
    Left(@Second, CharIndex(',', @Second) - 1),
    Substring(@Second, CharIndex(',', @Second) + 1, Len(@Second))
;
If your parameters might not always contain two values, you will need to add an appropriate fallback.


EDIT: Based on your updated question, you want to join the results of splitting both parameters. That's not quite as simple as it should be, since the STRING_SPLIT function doesn't return any sort of row number. But you can fake it with ROW_NUMBER:
SQL
WITH cteFirst As
(
    SELECT
        value,
        ROW_NUMBER() OVER (ORDER BY (SELECT 0)) As RN
    FROM
        STRING_SPLIT(@First, ',')
),
cteSecond As
(
    SELECT
        value,
        ROW_NUMBER() OVER (ORDER BY (SELECT 0)) As RN
    FROM
        STRING_SPLIT(@Second, ',')
),
cteData As
(
    SELECT
        F.value As First,
        S.value As Second
    FROM
        cteFirst As F
        INNER JOIN cteSecond As S
        ON S.RN = F.RN
)
INSERT INTO Table1 (Id, Field1, First, Second)
SELECT @Id, 0, First, Second
FROM cteData;
 
Share this answer
 
v2
Comments
Member 14800672 10-Aug-21 6:58am    
Hi Richard I updated the question in what i have tried section

I don't want to insert only the first two. i need to insert records in the table based on the number of items inside the string split (please note that @First and @Second will always contain equal counts, i mean if there is 5 values in @First, then there will be 5 different values in @Second)
Richard Deeming 10-Aug-21 7:10am    
See my updated solution, from the "Edit" part onwards.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900