批量update sql

批量更新sql |批量update sql
2008年05月21日 星期三 16:36
批量更新表时,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));

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值