Sql 如何从另一个表更新记录

Sql 如何从另一个表更新记录,sql,sql-server,sql-update,Sql,Sql Server,Sql Update,表1 update table1 set LoC = Select LoC from table2 表2 ID Loc ----------- 001 null 002 null 003 PYD 004 null .... 我想从表2中更新表1。我需要这样的东西: ID Loc ----------- 001 TMB 002 null 003 PYD 004 SHD .... 以及预期产量: 表1 update table1

表1

update table1 set LoC = Select LoC from table2
表2

ID     Loc
-----------
001    null
002    null
003    PYD
004    null
....
我想从表2中更新表1。我需要这样的东西:

ID     Loc
-----------
001    TMB
002    null
003    PYD
004    SHD
....
以及预期产量:

表1

update table1 set LoC = Select LoC from table2
如何从表2更新表1?

您可以使用联接

ID     Loc
-----------
001    TMB
002    null
003    PYD
004    SHD
....
update table1 set LoC = (Select LoC from table2 where table2 .ID =  table1 .ID)
可以使用连接

update table1 set LoC = (Select LoC from table2 where table2 .ID =  table1 .ID)
这就是你需要的吗

update t1 set t1.Loc=t2.Loc
From Table1 t1 inner join Table2 t2
on t1.Id=t2.Id
这就是你需要的吗

update t1 set t1.Loc=t2.Loc
From Table1 t1 inner join Table2 t2
on t1.Id=t2.Id
尝试以下方法:

update table1
set Loc = t2.Loc
from table1 t1 join table2 t2
   on t1.ID = t2.ID
尝试以下方法:

update table1
set Loc = t2.Loc
from table1 t1 join table2 t2
   on t1.ID = t2.ID

Join需要on,而不是where子句。Join需要on,而不是where子句。为什么删除sql-server-2000标记?这个问题不是针对sql-server-2000的吗?为什么删除了sql-server-2000标记?这个问题不是针对sql-server-2000的吗?