update关联其他表批量更新数据-跨数据库-跨服务器Update时关联表条件更新

1.有时在做项目时会有些期初数据更新,从老系统更新到新系统。如果用程序循环从老系统付给新系统。

2.有时在项目中需要同步程序,或者自动同步程序时会有大量数据更新就可能用到如下方法了。

3.为了做分析,需要整合一些数据到一个表中,涉及到通过主键关联多个表,获取其中的某些字段的值,通过update可以简单实现,找了一篇文章,挺不错:

    批量更新表时,update一个表的列时,需要依赖另外的表,这种依赖可以是where条件子句,也可以要update的field的值依赖另外的表
通常有两种做法
   1.使用存储过程
   2.在程序代码里逐条循环执行
 3.有一种更高效、简洁的做法,批量更新SQL ,一句SQL就可以替代麻烦的循环过程,有MS SQLServer、Oracle、DB2下的写法

UPDATE kcm02 
SET kcjhcb = V_U8_Inventory.iInvRCost,kccbhs = V_U8_Inventory.cValueType
FROM  V_U8_Inventory 
WHERE V_U8_Inventory.cInvCode = kcm02.kcwpdm

 另如果需要跨库跨服务器更新的时候用如下语法:跨库设置参照:http://www.cnblogs.com/spring_wang/p/3370750.html

update kcm02 
set kcjhcb = Inventory.kcjhcb,kccbhs = Inventory.kccbhs
 from  OPENDATASOURCE ('SQLOLEDB', 
                      'Data Source=192.168.1.3; 
                          USER ID = sa ;Password=123' ).yzerp.dbo.kcm02 as Inventory 
where Inventory.kcwpdm = kcm02.kcwpdm 

 

 

--其它参考如下----------------------------------------------------
--关键点: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));

 

作者:王春天 2013.11.12  地址:http://www.cnblogs.com/spring_wang/p/3418903.html

posted @ 2013-11-12 09:24  王春天  阅读(6032)  评论(0编辑  收藏  举报
云推荐