SQL Server accesses data across servers

Source: Internet
Author: User
Tags one table

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

Related Article

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.