Copy codeThe Code is as follows:
. Cursor Mode 1 DECLARE @ Data NVARCHAR (max)
SET @ Data = '1, tanw; 2, keenboy '-- Id, Name
DECLARE @ dataItem NVARCHAR (100)
DECLARE data_cursor cursor for (SELECT * FROM split (@ Data ,';'))
OPEN data_cursor
Fetch next from data_cursor INTO @ dataItem
WHILE @ FETCH_STATUS = 0
BEGIN
DECLARE @ Id INT
DECLARE @ Name NVARCHAR (50)
DECLARE dataItem_cursor cursor for (SELECT * FROM split (@ dataItem ,','))
OPEN dataItem_cursor
Fetch next from dataItem_cursor INTO @ Id
Fetch next from dataItem_cursor INTO @ Name
CLOSE dataItem_cursor
DEALLOCATE dataItem_cursor
/*
Perform logical processing, insert, or update operations here...
*/
END
CLOSE data_cursor
DEALLOCATE data_cursor
Copy codeThe Code is as follows:
. While Mode
DECLARE @ Data NVARCHAR (max)
SET @ Data = 'tanw, keenboy '-- Id, Name
DECLARE @ Temp TABLE
(
Id int identity (1, 1 ),
Name NVARCHAR (50)
)
DECLARE @ Id INT
DECLARE @ Name NVARCHAR (50)
DECLARE @ Results NVARCHAR (MAX) SET @ Results =''
Insert into @ Temp SELECT (SELECT * FROM split (@ Data ,';'))
While exists (SELECT * FROM @ Temp)
BEGIN
Select top 1 @ Id = Id, @ Name = Name from @ Temp
Delete from @ Temp where [id] = @ Id
SET @ Results = @ Results + @ Name + ','
/*
Perform logical processing, insert, or update operations here...
*/
END
SELECT @ Results
For a simple single table batch insert operation, the above method is not required