UPDATE/JOIN中的另一台服务器上的表中引用列(SQL Server)

问题描述:

我熟悉4部分命名,但每次尝试引用列时都会出错。例如:UPDATE/JOIN中的另一台服务器上的表中引用列(SQL Server)

UPDATE my_table 
SET my_table.column1 = otherserver.otherdatabase.dbo.othertable.column1 
FROM my_table INNER JOIN otherserver.otherdatabase.dbo.othertable 
ON my_table.column2 = otherserver.otherdatabase.dbo.othertable.column2 

这引发以下错误:

The multi-part identifier "otherserver.otherdatabase.dbo.othertable.column1" could not be bound.

我永远不会有麻烦,如果我只引用一个表,但是当我追加的列名,它总是抛出一个错误。有任何想法吗? SQL Server 2008中

+1

你能看到'select column1 from otherserver.otherdatabase.dbo.othertable'吗? – garik 2012-03-13 13:28:41

+0

是的,这工作正常。 – eek142 2012-03-13 13:32:27

只需使用表名时,你有资格的列。

UPDATE my_table 
SET my_table.column1 = othertable.column1 
FROM my_table INNER JOIN otherserver.otherdatabase.dbo.othertable 
ON my_table.column2 = othertable.column2 

或使用别名。

UPDATE my_table 
SET my_table.column1 = OT.column1 
FROM my_table INNER JOIN otherserver.otherdatabase.dbo.othertable as OT 
ON my_table.column2 = OT.column2 
+0

它的工作!我知道有一个简单的方法来做到这一点。谢谢!! – eek142 2012-03-13 13:37:13

+0

谢谢你,这让我疯狂。我忘记了架构(dbo)的一部分! – Avagut 2015-11-06 08:17:17

使用表的别名,然后访问列:

UPDATE my_table 
SET my_table.column1 = A.column1 
FROM my_table INNER JOIN otherserver.otherdatabase.dbo.othertable A 
ON my_table.column2 = A.column2 
+0

作品也很棒!谢谢! – eek142 2012-03-13 13:37:21