Bulk INSERT and update two solution sharing (stored procedures) in SQL Server _mssql

Source: Internet
Author: User
Tags bulk insert

1. Cursor Mode

Copy Code code as follows:

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

/*
Here to do the logical processing, insert or update operation ...
Www.jb51.net
*/
End
Close Data_cursor
Deallocate data_cursor

2.While Mode
Copy Code code as follows:

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 the =name from @Temp
DELETE from @Temp where [id] = @Id
SET @Results = @Results + @Name + ', '
Www.jb51.net
/*
Here to do the logical processing, insert or update operation ...
*/
End
SELECT @Results

If it is a simple single table bulk insert operation, the above method is very unnecessary

Author Keenboy

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.