Home >Database >Mysql Tutorial >How Can I Query Data Across Multiple SQL Servers?

How Can I Query Data Across Multiple SQL Servers?

Barbara Streisand
Barbara StreisandOriginal
2025-01-20 04:51:09348browse

How Can I Query Data Across Multiple SQL Servers?

Query data across different SQL Servers

By using linked servers, you can connect to multiple databases on different servers in a single SQL Server query.

Set up linked server

Access the linked server node in Object Explorer at:

<code>服务器对象 --> 链接服务器</code>

Or, execute the sp_addlinkedserver stored procedure.

Query linked table

To reference a table on a linked server in a query, use the following syntax:

<code>SELECT
    *
FROM
    本地表,
    [其他服务器名称].[其他数据库].[dbo].[其他表]</code>

If the schema name is different from the dbo, adjust as needed.

Example

Suppose there are two databases on different servers, containing tables named LocalTable and OtherTable. To perform a cross-server connection, execute the following query:

<code>SELECT
    *
FROM
    本地表
INNER JOIN
    [其他服务器名称].[其他数据库].[dbo].[其他表]
    ON 本地表.ID = 其他表.ID</code>

The above is the detailed content of How Can I Query Data Across Multiple SQL Servers?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn