简体   繁体   English

从链接服务器表更新 SQL 服务器本地表列

[英]Update SQL server local table column from linked server table

I have local and a linked server tables in SSMS我在 SSMS 中有本地和链接的服务器表

  • local [Arc].[dbo].[Record]本地[Arc].[dbo].[Record]
  • linked server SQLSERVR.[ArcReport].[dbo].[RecordRemote]链接服务器SQLSERVR.[ArcReport].[dbo].[RecordRemote]

when I query当我查询

  • select * from [Arc].[dbo].[Record]
  • select * from SQLSERVR.[ArcReport].[dbo].[RecordRemote]
    It brings results.它带来了结果。

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 张图片)。

  1. You can not use a column in the JOIN only tables go there (see 1st image)您不能在 JOIN only 表 go 中使用列(见第一张图片)
  2. Since you defined and alias for SQLSERVER.[ArcReport].[dbo].[RecordRemote] as remoteR you must use it to access its fields ( see SQL Aliases for more info and 2nd image)由于您将 SQLSERVER.[ArcReport].[dbo].[RecordRemote] 定义为 remoteR 并别名,因此您必须使用它来访问其字段(有关更多信息和第二张图片,请参阅 SQL 别名 在此处输入图像描述 在此处输入图像描述

在此处输入图像描述

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM