当前位置:   article > 正文

Oracle中实现一次插入多条数据_oracle insert into 多条

oracle insert into 多条

一、需求描述

        在我们实际的业务场景中,由于单条插入的效率很低(每次都需要数据库资源连接关闭的开销),故需要实现一次性插入多条数据,用以提升数据插入的效率;

如下图是常见的单条插入数据:

二、Oracle实现一次插入多条数据

我这里以grade2表为例进行演示说明:

2.1、union all拼接查询

采用union all拼接查询方式实现一次向同一个表中插入多条数据语法:

  1. INSERT into 表名(字段名1,字段名2,字段名3,...)
  2. SELECT1,值2,值3,... from dual
  3. union all SELECT11,值21,值31,... from dual
  4. union all SELECT12,值22,值32,... from dual
  5. ...

示例(向同一个表同时插入6条不同内容的数据):

  1. INSERT into "grade2"("name","chinese","math","english")
  2. SELECT '张10',100,100,100 from dual
  3. union all SELECT '张20',101,101,101 from dual
  4. union all SELECT '张30',102,102,102 from dual
  5. union all SELECT '张40',103,103,103 from dual
  6. union all SELECT '张50',104,104,104 from dual
  7. union all SELECT '张60',105,105,105 from dual

2.2、insert all插入

采用insert all方式实现一次向同一个表中插入多条数据语法:

  1. insert all
  2. into 表名(字段名1,字段名2,字段名3,...) VALUES(值1,值2,值3,...)
  3. into 表名(字段名1,字段名2,字段名3,...) VALUES(值11,值21,值31,...)
  4. into 表名(字段名1,字段名2,字段名3,...) VALUES(值12,值22,值32,...)
  5. ...
  6. SELECT * from dual;

示例(向同一个表同时插入6条不同内容的数据):

  1. insert all
  2. into "grade2"("name","chinese","math","english") VALUES('张11',100,100,100)
  3. into "grade2"("name","chinese","math","english") VALUES('张21',101,101,101)
  4. into "grade2"("name","chinese","math","english") VALUES('张31',102,102,102)
  5. into "grade2"("name","chinese","math","english") VALUES('张41',103,103,103)
  6. into "grade2"("name","chinese","math","english") VALUES('张51',104,104,104)
  7. into "grade2"("name","chinese","math","english") VALUES('张61',105,105,105)
  8. SELECT * from dual;

insert all方式实现一次向多个表中插入多条数据语法:

  1. insert all
  2. into 表名1(字段名1,字段名2,字段名3,...) VALUES(值1,值2,值3,...)
  3. into 表名1(字段名1,字段名2,字段名3,...) VALUES(值11,值21,值31,...)
  4. into 表名1(字段名1,字段名2,字段名3,...) VALUES(值12,值22,值32,...)
  5. into 表名2(字段名1,字段名2,字段名3,...) VALUES(值1,值2,值3,...)
  6. into 表名2(字段名1,字段名2,字段名3,...) VALUES(值11,值21,值31,...)
  7. into 表名2(字段名1,字段名2,字段名3,...) VALUES(值12,值22,值32,...)
  8. into 表名3(字段名1,字段名2,字段名3,...) VALUES(值1,值2,值3,...)
  9. into 表名3(字段名1,字段名2,字段名3,...) VALUES(值11,值21,值31,...)
  10. into 表名3(字段名1,字段名2,字段名3,...) VALUES(值12,值22,值32,...)
  11. ...
  12. SELECT * from dual;

示例(向多个表同时插入6条不同内容的数据):

  1. insert all
  2. into "grade2"("name","chinese","math","english") VALUES('张11',100,100,100)
  3. into "grade2"("name","chinese","math","english") VALUES('张21',101,101,101)
  4. into "grade2"("name","chinese","math","english") VALUES('张31',102,102,102)
  5. into "grade"("id","name","course","score","sex","age") VALUES(11,'张11','语文',100,'男',26)
  6. into "grade"("id","name","course","score","sex","age") VALUES(12,'张21','数学',100,'男',26)
  7. into "grade"("id","name","course","score","sex","age") VALUES(13,'张31','英语',100,'男',26)
  8. SELECT * from dual;

 

注意:insert all语句里不能直接使用序列生成的值(因为即便每个into语句里都加上seq_test_insert.nextval也不会获得多个值):

  1. -- 创建序列
  2. create sequence seq_test_insert
  3. minvalue 1
  4. maxvalue 999999999999999999999999
  5. start with 1
  6. increment by 1
  7. cache 20;
  8. -- 获取下一个序列值
  9. SELECT seq_test_insert.nextval from dual;

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/583357
推荐阅读
相关标签
  

闽ICP备14008679号