SQL remote database connection for Data Operations

Source: Internet
Author: User

I wrote a Data Synchronization script yesterday and found that the two databases are not on the same server. Later I thought that the database had the function of querying the remote connection database, and I modified the script again. The remote connection database query script is as follows:

-- Connection parameters between servers: @ server writes a name for the server for use below; sqloledb does not need to be changed; @ datasrc is the IP address of the remote database
Exec sp_addlinkedserver @ Server = 'servername', @ provider = 'sqlodb', @ srvproduct = '', @ datasrc = '192. 168.85.20'
-- Server login parameter description: The @ rmtsrvname here should be the same as the @ server above; @ rmtuser Login Server Username; @ rmtpassword Login server password
Exec DBO. sp_add1_srvlogin @ rmtsrvname = n'servername', @ useself = n'false', @ locallogin = NULL, @ rmtuser = n'sa ', @ rmtpassword = n'123'

/* ----------- Abbreviated form ------------------
Exec DBO. sp_addmediaserver 'servername', '', 'sqlodb', '192. 168.85.20'
Exec sp_add1_srvlogin 'servername', 'false', null, 'sa ', '123'
------------------------------------*/

Go
/* ------------ Data operation zone ----------------*/
Select * from [servername]. [database name]. [DBO]. [Table name]
/* -------------- End ---------------------*/
Go
-- Disable server connection
Exec sp_droplinkedsrvlogin 'servername', null
Exec sp_dropserver 'servername'

The stored procedures used are available in "system database master!

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.