SqlServer add linked database

--View current link status

select * from sys.servers

--Use sp_helpserver to display available servers

Exec sp_helpserver

-- delete an existing link

Exec sp_droplinkedsrvlogin server alias, Null
Exec sp_dropserver server alias

--Use sp_addlinkedserver to add links

EXEC sp_addlinkedserver
@server = ' 192.168.2.66 ' ,-- the alias of the server being accessed (it is customary to use the target server IP directly, or take an alias such as: JOY)
@srvproduct='',
@provider='SQLOLEDB',
@datasrc = ' 192.168.2.66 ' -- the server to access

--Use sp_addlinkedsrvlogin to add user login links

EXEC sp_addlinkedsrvlogin
' 192.168.2.66 ' , -- the alias of the server being accessed (if the alias JOY is used in sp_addlinkedserver above, it is also JOY here)
 ' false ' ,
NULL,
' sa ' , --account
 ' test123 ' --password _

Example of use (access the database Music on the target server, and view the content of the table test):
If the alias when establishing the link is the target server IP , 
i.e. 192.168.2.66

but:

select * from [192.168.2.66].[Music].dbo.test
If the alias when establishing the link is JOY,

but:

select * from [JOY].[Music].dbo.test

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325020010&siteId=291194637