sql 链接服务器加入查询

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21137592/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 00:44:27  来源:igfitidea点击:

sql linked server join query

sqlsql-server-2008linked-server

提问by sd_dracula

I am having issues running any query on joining a local DB with a DB from a linked server.

我在运行有关将本地数据库与来自链接服务器的数据库连接的任何查询时遇到问题。

My query:

我的查询:

SELECT 

        [LocalDatabase].[dbo].[Record].[Project_ID],
        [LinkedServer].[Reporting].[dbo].[Active].[Name]

        FROM [LocalDatabase].[dbo].[Record] inner join 
             [LinkedServer].[Reporting].[dbo].[Active] ON
             [LocalDatabase].[dbo].[Record].[Project_ID] = [LinkedServer].[Reporting].[dbo].[Active].[Delivery_Number]

The errors:

错误:

Msg 4104, Level 16, State 1, Line 9
The multi-part identifier "LinkedServer.Reporting.dbo.Active.Delivery_Number" could not be bound.
Msg 4104, Level 16, State 1, Line 5
The multi-part identifier "LinkedServer.Reporting.dbo.Active.Name" could not be bound.

I am guessing my syntax is incorrect but I am unable to fix it. Can someone please suggest a solution?

我猜我的语法不正确,但我无法修复它。有人可以提出解决方案吗?

If there is a better solution for me to run a select query on 2 databases which are on different servers, please mention it.

如果有更好的解决方案让我在不同服务器上的 2 个数据库上运行选择查询,请提及它。

回答by Gordon Linoff

Try writing this using table aliases:

尝试使用表别名编写此代码:

SELECT r.[Project_ID], a.[Name]
FROM [LocalDatabase].[dbo].[Record] r inner join 
     [LinkedServer].[Reporting].[dbo].[Active] a
     ON r.[Project_ID] = a.[Delivery_Number];