Get the App
SLTechnology News&Howtos  ›  Database  › 

How does SQL Server 2014 use cursor loops to insert data into a remote database

Shulou Source: shulou.com Published: 2022-05-31 15:10:14 10月12日 Update

This article is about how SQL Server 2014 uses cursor loops to insert data into remote databases. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

First, set up a linked server in the remote database.

Set up the corresponding table structure on the remote database.

Taking CompanyID as the condition of table insertion, the cursor is designed.

Use the following SQL to achieve the circular insertion of cursors, to achieve batch insertion of data into the target database, and to avoid large transaction operations in the database.

Declare @ CompanyID Varchar (20)

Declare Cur_Company CURSOR FOR

Select distinct CompanyID From Crm_Bas_Table

OPEN Cur_Company

FETCH NEXT FROM Cur_Company Into @ CompanyID

While @ @ FETCH_STATUS = 0

Begin

Insert Into [10.0.10.100]. [CRM_His_DB] .[dbo] .[Crm _ Bas_Table]

(CompanyID,BillNo,CustomerID)

Select CompanyID,BillNo,CustomerID

From Crm_Bas_Table with (nolock)

Where CompanyID = @ CompanyID

FETCH NEXT FROM Cur_Company Into @ CompanyID

End

CLOSE Cur_Company

DEALLOCATE Cur_Company

Thank you for reading! This is the end of this article on "how SQL Server 2014 uses cursor loops to insert data into a remote database". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!

Tags: Data databases cursors loops content more articles good practical transactions articles servers conditions goals look knowledge structure links facets build Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Redmi MySQL Docker Shulou Tech Info