Chinaunix首页 | 论坛 | 博客
  • 博客访问: 463418
  • 博文数量: 141
  • 博客积分: 211
  • 博客等级: 入伍新兵
  • 技术积分: 1049
  • 用 户 组: 普通用户
  • 注册时间: 2010-09-17 16:25
个人简介

如此经年,望尽千帆。

文章分类

全部博文(141)

文章存档

2014年(73)

2013年(65)

2012年(3)

我的朋友

分类: SQLServer

2013-03-04 19:17:53

sql批量更新简单实例,有需要的朋友可以参考下。

先来看一个简单点的例子:
sql批量更新

以下是更深一些的有关sql批量更新的讲解。

sql批量更新表时,特别是update一个表的列时,需要依赖另外的表,这种依赖可以是where条件子句,也可以是update的field的值依赖另外的表

一般有两种做法:
1)、使用存储过程
2)、在程序代码里逐条循环执行

本文为大家提供一种效率更高的批量更新SQL的方法,一句SQL即可替代麻烦的循环过程,分为MS SQLServer、Oracle、DB2三个库的版本。


点击(此处)折叠或打开

  1. --关键点:t4和t1是同一个table,primary key肯定也是同一个,

  2. --并以它进行关联,这样在 select语句里即可引用到要update的表的fields

  3. --脚本学堂 原创
  4. UPDATE Table1 AS t1
  5. SET (Field1,Field2) = (SELECT Field21, Field22
  6.                        FROM Table2 t2
  7.                             INNER JOIN Table3 t3
  8.                                  ON t3.Field31 = t2.Field23
  9.                             INNER JOIN Table4 t4
  10.                                  ON t3.Field32 = t4.Filed41
  11.                        WHERE t2.Field24 >= ''
  12.                                       AND t1.fId = t4.fId);
  13. ----------------------------MS SQLServer --------------------------------------
  14. UPDATE t1
  15. SET Field1 = Field21, Field2 = Field22
  16. FROM Table2 t2
  17.          INNER JOIN Table3 t3
  18.               ON t3.Field31 = t2.Field23
  19.          INNER JOIN Table4 t4
  20.               ON t3.Field32 = t4.Filed41
  21. WHERE ((t2.Field24 >= '')
  22.                   AND t1.fId = t4.fId);
  23. ----------------------------Oracle --------------------------------------------
  24. UPDATE Table1 t1
  25. SET (Field1,Field2) = (SELECT Field21, Field22
  26.                        FROM Table2 t2
  27.                             INNER JOIN Table3 t3
  28.                                  ON t3.Field31 = t2.Field23
  29.                             INNER JOIN Table4 t4
  30.                                  ON t3.Field32 = t4.Filed41
  31.                        WHERE ((t2.Field24 >= '')
  32.                                  AND t1.fId = t4.fId))
  33. WHERE EXISTS (SELECT Field21, Field22
  34.               FROM Table2 t2
  35.                    INNER JOIN Table3 t3
  36.                         ON t3.Field31 = t2.Field23
  37.                    INNER JOIN Table4 t4
  38.                         ON t3.Field32 = t4.Filed41
  39.               WHERE ((t2.Field24 >= '')
  40.                                AND t1.fId = t4.fId));
  41. ---------------------------------DB2 ------------------------------------------
  42. UPDATE Table1 AS t1
  43. SET (Field1,Field2) = (SELECT Field21, Field22
  44.                        FROM Table2 t2
  45.                             INNER JOIN Table3 t3
  46.                                  ON t3.Field31 = t2.Field23
  47.                             INNER JOIN Table4 t4
  48.                                  ON t3.Field32 = t4.Filed41
  49.                        WHERE ((t2.Field24 >= '')
  50.                                          AND t1.fId = t4.fId))
  51. WHERE EXISTS (SELECT Field21, Field22
  52.               FROM Table2 t2
  53.                    INNER JOIN Table3 t3
  54.                         ON t3.Field31 = t2.Field23
  55.                    INNER JOIN Table4 t4
  56.                         ON t3.Field32 = t4.Filed41
  57.               WHERE ((t2.Field24 >= '')

  58.                                 AND t1.fId = t4.fId));

以上内容参考了部分网友的文章,在此感谢原作者。


本文原始链接:
阅读(3982) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~