Sql server 从另一个表更新多列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32181893/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 03:59:10  来源:igfitidea点击:

Sql server update multiple columns from another table

sqlsql-serversql-server-2008

提问by Klapsius

I have read lots of post about how to update multiple columns but still can't find right answer.

我已经阅读了很多关于如何更新多列的帖子,但仍然找不到正确的答案。

I have one table and I would like update this table from another table.

我有一张桌子,我想从另一张桌子更新这张桌子。

Update table1 
set (a,b,c,d,e,f,g,h,i,j,k)=(t2.a,t2.b,t2.c,t2.d,t2.e,t2.f,t2.g,t2.h,t2.i,t2.j,t2.k)
from 
(
  SELECT ..... with join ... where .... 

) t2
    where table1.id=table2.id

If I running only select statement (between brackets) then script return values but not working with update

如果我只运行 select 语句(在括号之间),则脚本返回值但不使用更新

回答by Lukasz Szozda

TSQL does not support row-value constructor. Use this instead:

TSQL 不支持行值构造函数。改用这个:

UPDATE table1 
SET a = t2.a,
    b = t2.b,
    (...)
FROM 
(
SELECT ..... with join ... WHERE .... 
) t2
WHERE table1.id = table2.id

回答by M.Ali

You don't need to use a sub-query you can also simply do the following....

您不需要使用子查询,您也可以简单地执行以下操作....

Update t1 
set t1.a  = t2.a
   ,t1.b  = t2.b
   ,t1.c  = t2.c
   ,t1.d  = t2.d
   .......
from table1 t1
JOIN table2 t2  ON t1.id = t2.id
WHERE .......

回答by Matz

The UPDATE SET commands implicitly apply on the table specified by , and it is not possible to specify the table on the SET operation.

UPDATE SET 命令隐式应用于由 指定的表,并且不可能在 SET 操作上指定该表。

Edit: Specify only the column name you want to update, do not mention the table.

编辑:仅指定要更新的列名,不提及表。