SQL-Server 2014。更新加入两个 table 并使用 table 别名

SQL-Server 2014. UPDATE joining two tables and using table aliases

我有两张几乎一模一样的桌子。 需要更新 TABLE_03 中的每个 Postal 当它是 NULL 没有要使用的 ID 或主键,所以我将它们与列 LinkNamePhoneNum /(Phone in TABLE_ALL)

尝试过SQL:

 UPDATE TABLE_03 a
   join TABLE_ALL b 
   ON   a.Link = b.Link,
        a.Name = b.Name,
        a.PhoneNum = b.Phone
   SET a.Postal = b.PostalCode
  WHERE a.Postal IS NULL;

SQL 服务器报错

Incorrect syntax near 'a'.

没有别名:

  UPDATE TABLE_03 
   join TABLE_ALL 
   ON   TABLE_03.Link = TABLE_ALL.Link,
        TABLE_03.Name = TABLE_ALL.Name,
        TABLE_03.PhoneNum = TABLE_ALL.Phone
   SET TABLE_03.Postal = TABLE_ALL.PostalCode
  WHERE TABLE_03.Postal IS NULL; 

给出错误:

Incorrect syntax near the keyword 'join'.

试试这个

       UPDATE TABLE_03 
 SET TABLE_03.Postal = TABLE_ALL.PostalCode
 from TABLE_ALL 
 where  TABLE_03.Link = TABLE_ALL.Link AND 
    TABLE_03.Name = TABLE_ALL.NAME AND 
    TABLE_03.PhoneNum = TABLE_ALL.Phone and

   TABLE_03.Postal IS NULL; 

您的语法错误,主要是 JOIN ON 子句。它应该像下面使用 AND operator

UPDATE TABLE_03 a
   join TABLE_ALL b 
   ON   a.Link = b.Link AND
        a.Name = b.Name AND
        a.PhoneNum = b.Phone
   SET a.Postal = b.PostalCode
  WHERE a.Postal IS NULL;

使用正确的 SQL Server 语法

UPDATE a
SET a.Postal = b.PostalCode
FROM TABLE_03 a
   join TABLE_ALL b 
   ON   a.Link = b.Link AND
        a.Name = b.Name AND
        a.PhoneNum = b.Phone   
  WHERE a.Postal IS NULL;

试试这个:

 UPDATE a
   SET a.Postal = b.PostalCode
   FROM TABLE_03 a
   join TABLE_ALL b 
   ON   a.Link = b.Link,
        a.Name = b.Name,
        a.PhoneNum = b.Phone
  WHERE a.Postal IS NULL;