SQL 中 Update 实现多表更新

SQL 中 Update 实现多表更新

SQL Server语法:

UPDATE { table_name WITH ( < table_hint_limited > [ ...n ] ) | view_name | rowset_function_limited } SET { column_name = { expression | DEFAULT | NULL } | @variable = expression | @variable = column = expression } [ ,...n ] { { [ FROM { < table_source > } [ ,...n ] ] [ WHERE < search_condition > ] } | [ WHERE CURRENT OF { { [ GLOBAL ] cursor_name } | cursor_variable_name } ] } [ OPTION ( < query_hint > [ ,...n ] ) ]

SQL Server示例:

update a

set a.gqdltks=b.gqdltks,a.bztks=b.bztks

from landleveldata a,gdqlpj b

where a.GEO_Code=b.lxqdm


有A、B两张表,其记录如下:

A表
  c1        c2
 --------------
  1          a1
  2          a2
  3          a3
  5          a5


B表
  c1       c3
 --------------
  1         b1
  2         b1
  3         b3
  4         b4

A.c1与B.c1相等,用一条sql语句,实现A.c2的值更新为B.c3
  ------------------------
  UPDATE    A
  SET A.c2 =B.c3
  from A ,B
  where A.c1=B.c1

  UPDATE    A
  SET A.c2 =B.c3
  from A
  inner join B on A.c1=B.c1

注意:update后面是不能跟多表的,但可以跟在from子句后面。

你可能感兴趣的:(SQL 中 Update 实现多表更新)