[英]Update SQL server local table column from linked server table
I have local and a linked server tables in SSMS我在 SSMS 中有本地和链接的服务器表
[Arc].[dbo].[Record]
[Arc].[dbo].[Record]
SQLSERVR.[ArcReport].[dbo].[RecordRemote]
SQLSERVR.[ArcReport].[dbo].[RecordRemote]
when I query当我查询
select * from [Arc].[dbo].[Record]
select * from SQLSERVR.[ArcReport].[dbo].[RecordRemote]
But when I query但是当我查询
UPDATE [Arc].[dbo].[Record]
SET [Arc].[dbo].[Record].Column1= SQLSERVER.[ArcReport].[dbo].[RecordRemote].Column2
WHERE [Arc].[dbo].[Record].id = 16
or或者
UPDATE localR
SET localR.Column1= remoteR.Column2
FROM [Arc].[dbo].[Record] AS localR
INNER JOIN SQLSERVER.[ArcReport].[dbo].[RecordRemote].Column2 AS remoteR
ON localR.id= [SQLSERVER].[ArcReport].[dbo].[RecordRemote].id
it says它说
The multi-part identifier "SQLSERVER.ArcReport.dbo.RecordRemote.Column2" could not be bound.无法绑定多部分标识符“SQLSERVER.ArcReport.dbo.RecordRemote.Column2”。
You have made a mistake in your SQL query (see 3rd image).您在 SQL 查询中犯了一个错误(参见第 3 张图片)。
You need to perform a join so that SQL Server knows what row to update from.您需要执行连接,以便 SQL 服务器知道要从哪一行更新。
UPDATE localR
SET localR.Column1 = remoteR.Column2
FROM Arc.dbo.[Record] AS localR
INNER JOIN LinkedServerName.ArcReport.dbo.RecordRemote AS remoteR
ON <some join condition between localR and remoteR>;
BTW your syntax wouldn't work locally, either.顺便说一句,您的语法在本地也不起作用。 It essentially boils down to:
它基本上归结为:
UPDATE dbo.T1
SET C1 = dbo.T2.C2;
This is simply invalid syntax.这只是无效的语法。 You can't surprise add a table reference like that.
您不会惊讶地添加这样的表引用。
Your join syntax is wrong:您的连接语法错误:
INNER JOIN SQLSERVER.[ArcReport].[dbo].[RecordRemote].Column2 AS remoteR <= column2 is not a table
When doing a join, you join 2 tables (not columns), with a join condition on these 2 tables:进行连接时,您连接 2 个表(不是列),在这 2 个表上有一个连接条件:
That makes:这使得:
UPDATE localR
SET localR.Column1= remoteR.Column2
FROM [Arc].[dbo].[Record] AS localR
INNER JOIN SQLSERVER.[ArcReport].[dbo].[RecordRemote] AS remoteR
ON localR.id= remoteR.id
In SQL server inner join is not necessary while updating a table from another table, where clause will do the trick.在 SQL 中,当从另一个表更新一个表时不需要服务器内部连接,where 子句可以解决问题。 The simplify solution for your problem would be:
您的问题的简化解决方案是:
UPDATE Record
SET Record.Column1= remoteR.Column2
FROM SQLSERVER.[ArcReport].[dbo].[RecordRemote] AS remoteR
where Record.id= [SQLSERVER].[ArcReport].[dbo].[RecordRemote].id and Record.id = 16
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.