关于tsql:在SQL Server中使用JOIN更新表?

Update a table using JOIN in SQL Server?

我想更新表中的一列,在其他表上进行联接,例如:

1
2
3
4
5
6
UPDATE table1 a
INNER JOIN table2 b ON a.commonfield = b.[common field]
SET a.CalculatedColumn= b.[Calculated Column]
WHERE
    b.[common field]= a.commonfield
AND a.BatchNO = '110'

但它在抱怨:

Msg 170, Level 15, State 1, Line 2
Line 2: Incorrect syntax near 'a'.

这里怎么了?


您不太了解SQL Server的专有UPDATE FROM语法。也不知道为什么你需要加入CommonField,然后还要过滤它。试试这个:

1
2
3
4
5
6
UPDATE t1
  SET t1.CalculatedColumn = t2.[Calculated Column]
  FROM dbo.Table1 AS t1
  INNER JOIN dbo.Table2 AS t2
  ON t1.CommonField = t2.[Common Field]
  WHERE t1.BatchNo = '110';

如果您在做一些非常愚蠢的事情,例如不断尝试将一列的值设置为另一列的聚合(这违反了避免存储冗余数据的原则),则可以使用CTE(公共表表达式)-有关详细信息,请参阅此处和此处:

1
2
3
4
5
6
7
8
9
10
11
;WITH t2 AS
(
  SELECT [key], CalculatedColumn = SUM(some_column)
    FROM dbo.table2
    GROUP BY [key]
)
UPDATE t1
  SET t1.CalculatedColumn = t2.CalculatedColumn
  FROM dbo.table1 AS t1
  INNER JOIN t2
  ON t1.[key] = t2.[key];

这真是愚蠢的原因,因为每次在table2中的任何一行发生更改时,都必须重新运行整个更新。SUM是一个可以在运行时计算的东西,这样做,就不必担心结果过时。


尝试如下:

1
2
3
4
5
6
begin tran
    UPDATE a
    SET a.CalculatedColumn= b.[Calculated Column]
    FROM table1 a INNER JOIN table2 b ON a.commonfield = b.[common field]
    WHERE a.BatchNO = '110'
commit tran


亚伦以上的回答是完全的。

1
2
3
4
5
6
UPDATE a
  SET a.CalculatedColumn = b.[Calculated Column]
  FROM Table1 AS a
  INNER JOIN Table2 AS b
  ON a.CommonField = b.[Common Field]
  WHERE a.BatchNo = '110';

只想添加当我们在更新表时尝试使用表的别名时SQL Server中出现此问题的原因,下面提到的语法将始终给出错误:

1
2
3
update tableName t
set t.name = 'books new'
where t.id = 1

如果正在更新单个表或在使用join时进行更新,那么case可以是any。

尽管上面的查询在pl/sql中可以正常工作,但在SQL Server中却不行。

在SQL Server中使用表别名时更新表的正确方法是:

1
2
3
4
update t
set t.name = 'books new'
from tableName t
where t.id = 1

希望它能帮助大家理解为什么错误会出现在这里。


1
2
3
4
5
6
7
MERGE table1 T
   USING table2 S
      ON T.CommonField = S."Common Field"
         AND T.BatchNo = '110'
WHEN MATCHED THEN
   UPDATE
      SET CalculatedColumn = S."Calculated Column";

1
2
3
4
5
6
7
8
9
    UPDATE mytable
         SET myfield = CASE other_field
             WHEN 1 THEN 'value'
             WHEN 2 THEN 'value'
             WHEN 3 THEN 'value'
         END
    From mytable
    Join otherTable on otherTable.id = mytable.id
    Where othertable.somecolumn = '1234'

更多备选方案:http://www.karlrixon.co.uk/writing/update-multiple-rows-with-different-values-and-a-single-sql-query/


似乎SQL Server 2012也可以处理Teradata的旧更新语法:

1
2
3
4
5
6
UPDATE a
SET a.CalculatedColumn= b.[Calculated Column]
FROM table1 a, table2 b
WHERE
    b.[common field]= a.commonfield
AND a.BatchNO = '110'

如果我没记错的话,2008R2在我尝试类似的查询时出错了。


我发现将一个更新转换成一个select来获取要在更新之前作为测试更新的行是很有用的。如果我可以选择我想要的确切行,我可以只更新那些我想要更新的行。

1
2
3
4
5
6
7
8
9
10
11
12
13
DECLARE @expense_report_id AS INT
SET @expense_report_id = 1027

--UPDATE expense_report_detail_distribution
--SET service_bill_id = 9

SELECT *
FROM expense_report_detail_distribution erdd
INNER JOIN expense_report_detail erd
INNER JOIN expense_report er
    ON er.expense_report_id = erd.expense_report_id
    ON erdd.expense_report_detail_id = erd.expense_report_detail_id
WHERE er.expense_report_id = @expense_report_id

我也有同样的问题……您不需要添加物理列。因为现在你必须维护它。您可以在选择查询中添加一个通用列:

前任:

1
2
3
4
5
6
select tb1.col1, tb1.col2, tb1.col3 ,
(
select 'Match' from table2 as tbl2
where tbl1.col1 = tbl2.col1 and tab1.col2 = tbl2.col2
)  
from myTable as tbl1

另一种方法是使用合并

1
2
3
4
5
6
7
8
9
10
  ;WITH cteTable1(CalculatedColumn, CommonField)
  AS
  (
    select CalculatedColumn, CommonField from Table1 Where BatchNo = '110'
  )
  MERGE cteTable1 AS target
    USING (select"Calculated Column","Common Field" FROM dbo.Table2) AS source ("Calculated Column","Common Field")
    ON (target.CommonField = source."Common Field")
    WHEN MATCHED THEN
        UPDATE SET target.CalculatedColumn = source."Calculated Column";

-合并是SQL标准的一部分

-另外,我很确定内部连接更新是不确定的。这里有个类似的问题,答案是什么?http://ask.sqlservercentral.com/questions/19089/updating-two-tables-using-single-query.html


尝试:

1
2
3
4
5
UPDATE table1
SET CalculatedColumn = ( SELECT [Calculated Column]
                         FROM table2
                         WHERE table1.commonfield = [common field])
WHERE  BatchNO = '110'