SQL Server inserts data across Databases

Source: Internet
Author: User

For the company's project transformation, we need to import the previous database table records to the new database table. The structure is not exactly the same.

Many things have been learned in the Cross-database process. It turns out that sqlserver has the function of linking servers.

 
/* Data operations between different Server databases */

 
-- Create a linked server

 
ExecSp_addmediaserver'Itsv','','Sqlodb','Remote server name or IP address' 

 
ExecSp_add1_srvlogin'Itsv','False',Null,'Username','Password' 

 
-- Query example

 
Select*FromItsv. Database Name. DBO. Table Name

 
-- Import example

 
Select*IntoTableFromItsv. Database Name. DBO. Table Name

 
-- Delete the linked server when it is no longer in use

 
ExecSp_dropserver'Itsv','Droplogins' 

My tests:

ExecSp_addmediaserver'Srv _ lnk','','Sqlodb','192. 168.1.x1' 

 
ExecSp_add1_srvlogin'Srv _ lnk','False',Null,'Sa','Sa'

 
 

 
InsertIntoCounterdb. DBO. Sites (sitekey, parentid, name)

SelectBarid, areacode, barnameFromSrv_lnk.testcounter.dbo.site

 
 

 
InsertIntoCounterdb. DBO. countdata

 
(Camerakey, counttime, innum, outnum, refix, exportflag, changeflag)

 
Select Top(200000) camerano, datetime, up, down, refix

 
FromSrv_lnk.testcounter.dbo.counter_table

 
 

ExecSp_dropserver'Srv _ lnk','Droplogins' 

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.