Requirements: Two identical tables, distributed on two server databases, now in one table, view the contents of the two tables and sort the IDs
1: In the local database Query Analyzer, run the following two-segment statement:
--Create a linked server
EXEC sp_addlinkedserver
@server = ' Otherdb ', an alias for the server to be linked to, or the following IP address
@srvproduct = ",--empty
@provider = ' SQLOLEDB ',--no need to modify
@datasrc = ' 192.168.1.1 '--the server IP that needs to be connected, if the linked server database has the added port number, it should be changed to ' 192.168.1.1, port number '
--Login to the linked server
EXEC sp_addlinkedsrvlogin
' Otherdb ',--the server alias that was taken above
' False ',
Null
' username ',--account number--can be a user of a database on the server, not necessarily SA
' Password '--password
2: Federated Query
Select Id,a,b from table1 Union(select Id,a,b from [otherdb].[ Name].dbo.table1 as Table2) ORDER by ID
SQL Server accesses data across servers