I have a very simple operation, but I don't know how to proceed. In a Stored Procedure I get a string full of Id's separated by commas, so I need to separate that string and use its content inside a INSERT
. But I don't know how to use each part of said split
this is what I have:
select * from string_split(@listaDePagosErrores, ',')
--ahora quiero usar los splits en un insert
INSERT INTO[dbo].[ProgramacionIngresos]
(IdError)
VALUES
(split[1]) -- ? ? ? ? o algo así ?
SELECT * FROM [ProgramacionIngresos]
As I mentioned in the comments. You will not be able to use the function
STRING_SPLIT
since it was introduced as of version 13 (SQL Server 2016) and you have version 11.You're going to have to build your own function. In this Stack Overflow answer there is a function that can help you just what you want:
To make use of the function would be:
Up to this point you only have one table and each record separated by the value declared in the function. To make the
INSERT
from aSELECT
would be as follows:There are many ways to split a chain; I suggest that inside a CURSOR you use a function to split and store the values in a TABLE, then you do a WHILE to the cursor and inside it you do some INSERT'S until the loop is finished.
I leave you some examples:
Here the FUNCTION :
Here the CURSOR:
Here you open and close the CURSOR to do the WHILE :
Try and tell me how it went.
The string_split function is available starting with version 2016 of SQL Server. That said, its implementation would be as follows: