如何在一个sql语句中使用两个服务器?

时间:2013-06-24 19:52:34

标签: sql sql-server

所以我需要在一个SQL语句中尝试使用两个不同的服务器。这是我到目前为止的查询:

SELECT p.FirstField 
from [FirstDatabase]..FirstTableName p 
Where not exists (select p2.SecondField 
from [SecondServer].[SecondDatabase]..SecondTableName p2 
where p.FirstField = p2.SecondField)

我没有在语句的第一部分使用First Server,因为已经选择了该服务器。

我正在使用Microsoft SQL Server

1 个答案:

答案 0 :(得分:5)

您需要使用sp_addlinkedserver将远程服务器设置为链接服务器。然后,您可以使用linked_server_name.catalog.schema.object_name语法引用远程表。您可以找到更多信息here