批量更新sql 批量update sql

2021-08-25 12:32:32 字數 2279 閱讀 8399

批量更新sql |批量update sql

批量更新表時,update乙個表的列時,需要依賴另外的表,這種依賴可以是where條件子句,也可以要update的field的值依賴另外的表

通常有兩種做法

1.使用儲存過程

2.在程式**裡逐條迴圈執行

這裡給出一種更高效、簡潔的做法,批量更新sql ,一句sql就可以替代麻煩的迴圈過程,有ms sqlserver、oracle、db2下的寫法

--關鍵點:t4和t1是同乙個table,primary key肯定也是同乙個,

--並以它進行關聯,這樣在 select語句裡即可引用到要update的表的fields

update table1 as t1

set (field1,field2) = (select field21, field22

from table2 t2

inner join table3 t3

on t3.field31 = t2.field23

inner join table4 t4

on t3.field32 = t4.filed41

where t2.field24 >= ''

and t1.fid = t4.fid);

----------------------------ms sqlserver --------------------------------------

update t1

set field1 = field21, field2 = field22

from table2 t2

inner join table3 t3

on t3.field31 = t2.field23

inner join table4 t4

on t3.field32 = t4.filed41

where ((t2.field24 >= '')

and t1.fid = t4.fid);

----------------------------oracle --------------------------------------------

update table1 t1

set (field1,field2) = (select field21, field22

from table2 t2

inner join table3 t3

on t3.field31 = t2.field23

inner join table4 t4

on t3.field32 = t4.filed41

where ((t2.field24 >= '')

and t1.fid = t4.fid))

where exists (select field21, field22

from table2 t2

inner join table3 t3

on t3.field31 = t2.field23

inner join table4 t4

on t3.field32 = t4.filed41

where ((t2.field24 >= '')

and t1.fid = t4.fid));

---------------------------------db2 ------------------------------------------

update table1 as t1

set (field1,field2) = (select field21, field22

from table2 t2

inner join table3 t3

on t3.field31 = t2.field23

inner join table4 t4

on t3.field32 = t4.filed41

where ((t2.field24 >= '')

and t1.fid = t4.fid))

where exists (select field21, field22

from table2 t2

inner join table3 t3

on t3.field31 = t2.field23

inner join table4 t4

on t3.field32 = t4.filed41

where ((t2.field24 >= '')

and t1.fid = t4.fid));

sql批量更新

批量更新 1 statement statement cn.createstatement addbatch sql1 addbatch sql2 executebatch 乙個statement物件,可以執行多個sql語句以後,批量更新。這多個語句可以是delete update insert等或...

sql批量更新

批量更新 mysql更新語句很簡單,更新一條資料的某個字段,一般這樣寫 複製 如下 update mytable set myfield value where other field other value 如果更新同一欄位為同乙個值,mysql也很簡單,修改下where即可 複製 如下 upda...

SQL批量更新

如下 怎樣寫成一句,或中間用什麼字元隔開才能同時執行?update yao article set author 1 35 where author 山東 歷下 update yao article set author 1 36 where author 山東 市中 update yao arti...