IT俱乐部 Oracle Oracle中实现一次插入多条数据详细代码举例

Oracle中实现一次插入多条数据详细代码举例

一、需求描述

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

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

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

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

2.1、union all拼接查询

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

INSERT into 表名(字段名1,字段名2,字段名3,...) 
SELECT 值1,值2,值3,... from dual
union all SELECT 值11,值21,值31,... from dual
union all SELECT 值12,值22,值32,... from dual
...

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

INSERT into "grade2"("name","chinese","math","english") 
SELECT '张10',100,100,100 from dual
union all SELECT '张20',101,101,101 from dual
union all SELECT '张30',102,102,102 from dual
union all SELECT '张40',103,103,103 from dual
union all SELECT '张50',104,104,104 from dual
union all SELECT '张60',105,105,105 from dual

2.2、insert all插入

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

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

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

insert all 
into "grade2"("name","chinese","math","english") VALUES('张11',100,100,100)
into "grade2"("name","chinese","math","english") VALUES('张21',101,101,101)
into "grade2"("name","chinese","math","english") VALUES('张31',102,102,102)
into "grade2"("name","chinese","math","english") VALUES('张41',103,103,103)
into "grade2"("name","chinese","math","english") VALUES('张51',104,104,104)
into "grade2"("name","chinese","math","english") VALUES('张61',105,105,105)
SELECT * from dual;

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

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

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

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

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

-- 创建序列
create sequence seq_test_insert 
minvalue 1
maxvalue 999999999999999999999999
start with 1
increment by 1
cache 20;

-- 获取下一个序列值
SELECT seq_test_insert.nextval from dual;

总结 

到此这篇关于Oracle中实现一次插入多条数据的文章就介绍到这了,更多相关Oracle一次插入多条数据内容请搜索IT俱乐部以前的文章或继续浏览下面的相关文章希望大家以后多多支持IT俱乐部!

本文收集自网络,不代表IT俱乐部立场,转载请注明出处。https://www.2it.club/database/oracle/11898.html
上一篇
下一篇
联系我们

联系我们

在线咨询: QQ交谈

邮箱: 1120393934@qq.com

工作时间:周一至周五,9:00-17:30,节假日休息

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部