如何从表t2更新t1的列值?

时间:2022-02-02 00:46:40

I want to select t2 column values (Name) and update with t1 column values (Name) without where clause. what will be the query?

我想选择t2列值(Name)并使用t1列值(Name)更新而不使用where子句。什么是查询?

I am executing below code:

我正在执行下面的代码:

update t1 set t1.name=t2.name from t2 where t1.id=t2.id

but I want to perform it without WHERE clause.

但我想在没有WHERE子句的情况下执行它。

3 个解决方案

#1


2  

Join both tables on id:

在id上加入两个表:

UPDATE t1 SET t1.Name = t2.Name
FROM TableName1 t1
INNER JOIN TableName2 t2 ON t1.Id = t2.Id

#2


1  

We use MERGE nowadays:

我们现在使用MERGE:

MERGE t1 USING t2 ON t1.id = t2.id
WHEN MATCHED THEN
UPDATE SET name = t2.name;

#3


1  

just do it

去做就对了

UPDATE t1
SET t1.name=t2.name
FROM table1 t1 INNER JOIN table2 t2 ON t1.id=t2.id

#1


2  

Join both tables on id:

在id上加入两个表:

UPDATE t1 SET t1.Name = t2.Name
FROM TableName1 t1
INNER JOIN TableName2 t2 ON t1.Id = t2.Id

#2


1  

We use MERGE nowadays:

我们现在使用MERGE:

MERGE t1 USING t2 ON t1.id = t2.id
WHEN MATCHED THEN
UPDATE SET name = t2.name;

#3


1  

just do it

去做就对了

UPDATE t1
SET t1.name=t2.name
FROM table1 t1 INNER JOIN table2 t2 ON t1.id=t2.id