Operate the database across servers? it's actually really easy! (on)

Today I will share with you how to operate a database across servers, or to use SQL Server Management Tools (SSMS) as a platform for operation.

What is cross-server operation?

Cross-server operation means that you can connect locally to the database on the remote server, and you can perform related database operations on the other party's database, such as adding, deleting, modifying, and checking.

Why do cross-server operations

With the increase of data volume and the expansion of business volume, different databases need to be installed on different servers. Sometimes, because of business needs, the data in different servers need to be integrated. At this time, cross-server operations are required.

What are the tools for cross-server operations?

DBLINK (database link), as the name suggests, is the link to the database, just like a telephone line, it is a channel, when we want to cross the local database to access the data in another database table, the local database must create a remote database dblink , Through the dblink local database, the data in the remote database table can be accessed like a local database.

Well, not much nonsense, follow me to create DBLINK step by step.

Create SQL Server remote link method one

The first step is to
open SSMS—>log in to the local database—>server object—>link server (right click)—>create a new link server, as shown in Figure 1:

Operate the database across servers?  it's actually really easy!  (on)

Figure 1 Start to create a linked server

The second step
is to enter relevant information in the pop-up dialog box

  • Enter the IP address of the other party's server in [Linked Server]

  • Select [SQL Server] in [Server Type]

As shown in Figure 2:

Operate the database across servers?  it's actually really easy!  (on)

Figure 2 Enter general information

The third step is to
click [Security] on the left, and the following page will appear. In step 3, enter the account and password of the other party's database. As shown in Figure 3:

Operate the database across servers?  it's actually really easy!  (on)

Figure 3 Enter the other party's database account password

After clicking OK, the creation is successful, as shown in Figure 4 below, you can see the created link server

Operate the database across servers?  it's actually really easy!  (on)

Figure 4 Created DBLINK

After the creation is completed, the relevant code will be automatically generated, and the password is hidden with #:


EXEC master.dbo.sp_addlinkedserver 
@server = N'192.168.110.189', 
@srvproduct=N'SQL Server';

EXEC master.dbo.sp_addlinkedsrvlogin
@rmtsrvname=N'192.168.110.189',
@useself=N'False',
@locallogin=NULL,
@rmtuser=N'sa',
@rmtpassword='########'

We can use the created DBLINK to link to the other server

Let's use the created and try to query the table on the other's server to verify it.

SELECT * FROM 
[192.168.110.189].[erp25new].[dbo].[fee_data]

Following the FROM field above is [DBLINK name].[the counterparty database name].[the counterparty database schema name].[the counterparty database table name], and none of these contents before the table name is missing. The query results are as follows:

Operate the database across servers?  it's actually really easy!  (on)

Figure 5 Query results

Create SQL Server remote link method two

The first step of one installation method is to open the new link server and start the following operations.

first step

Follow the steps below to fill in the relevant information in the pop-up dialog box, as shown in Figure 6 below:

  • [Link server] Fill in the link name, preferably the server IP, for easy identification, and a custom name can also be used here.

  • [Server Type] Choose other data sources

  • Select the options in the figure in the [provider]

  • 【Product Name】Fill in custom content, or leave it blank

  • [Data source] Fill in the IP address of the other party's server

Operate the database across servers?  it's actually really easy!  (on)

Enter general information as shown in Figure 6

The second step
is similar to the method one, just fill in the account and password of the other party's server in the security field.

Operate the database across servers?  it's actually really easy!  (on)

After clicking OK, the creation is successful, as shown in Figure 7 below, you can see the created link server:

Operate the database across servers?  it's actually really easy!  (on)

Figure 7 DBLINK created

After the creation is completed, the relevant code will be automatically generated, and the password is hidden with #:

EXEC master.dbo.sp_addlinkedserver 
@server = N'TEST_SQL_SERVER', 
@srvproduct=N'TEST',
@provider=N'SQLNCLI11', 
@datasrc=N'192.168.110.189'

EXEC master.dbo.sp_addlinkedsrvlogin 
@rmtsrvname=N'TEST_SQL_SERVER',
@useself=N'False',
@locallogin=NULL,
@rmtuser=N'sa',
@rmtpassword='########'

For the meaning of the fields in the above code, please refer to my other tweet, the link is as follows:

SQL Server learning road (5)-the usage of DBLINK

The verification method for querying the other party’s database is similar to Method 1, except that the database name is changed to a custom name:


SELECT * FROM 
[TEST_SQL_SERVER].[erp25new].[dbo].[fee_data]

Well, I will introduce it here today, and next time I will explain to you the specific operation method of linking from SQL Server to Oracle and MYSQL, remember to pay attention~

Guess you like

Origin blog.51cto.com/15057820/2656422