-- check if server exists in table sys.server
select * from sys.servers
-- set database security
EXEC sp_configure 'show advanced options', 1
RECONFIGURE
GO
EXEC sp_configure 'ad hoc distributed queries', 1
RECONFIGURE
GO
-- add the external dbserver
EXEC sp_addlinkedserver @server='#servername#'
-- add login on external server
EXEC sp_addlinkedsrvlogin '#Servername#', 'false', NULL, '#username#', '#password@123"'
-- control query on remote table
select top (1000) * from [#server#].[#database#].[#schema#].[#table#]