oracle批量插入数据

本文介绍Oracle数据库中批量插入数据的三种方法:使用union all拼接查询、insert all语句及结合触发器自动设置ID,以及利用ibatis框架结合Oracle进行批量数据插入。适合于需要高效处理大量数据插入操作的数据库管理员和技术人员。

oracle一次插入多条的方法

1.采用union all拼接查询方式
insert into table_name(ID,NAME,AME)
          select 001,'张三',21 from dual
union all select 002,'Multi',31 from dual
2.采用insert all的方法

由于insert all方式插入多条时,通过sequence获取的值是同一个,不会自动获取多个,所以id需要通过其他方式设置,(我这里采用触发器方式自动设置id)
(1).创建表

create or replace trigger tr_test_insert
before insert on test_insert
for each row
begin
  select seq_test_insert.nextval into :new.data_id from dual;
end;  

(2).创建序列

create sequence seq_test_insert 
minvalue 1
maxvalue 999999999999999999999999
start with 1
increment by 1
cache 20;

(3).创建触发器
通过触发器自动给insert语句设置id值

create or replace trigger tr_test_insert
before insert on test_insert
for each row
begin
  select seq_test_insert.nextval into :new.data_id from dual;
end; 

(4).插入测试数据:

insert all 
into test_insert(user_name,address) values('aaa','henan')
into test_insert(user_name,address) values('bbb','shanghai')
into test_insert(user_name,address) values('ccc','beijing')
select * from dual;

另外,insert all还支持往不同的表里插入数据,如:

insert all 
into table1(filed1,filed2)values('value1','value2')
into table2(字段1,字段2,字段3) values(1,值2,值3)
select * from dual;
3.ibatis结合oracle批量插入数据
<insert id="insert_table" parameterClass="java.util.List">
insert  into sj_test( col1 , col2 , col3 ) values   select 
col1 , col2 , col3 
from (
<iterate conjunction=" union all ">
select 
#test[].col1# as col1  , #test []. col2# as col2, # test[].col3#  as col3  from dual
</iterate>
)
</insert>

使用这种方法需要向dao层传递一个List集合

Oracle数据库批量插入数据有多种方法,以下是几种常用的方式: 1. **使用`insert all`语句**: ```sql insert all into oracle_table ( id, code ) values( 1 , '1' ) into oracle_table ( id, code ) values( 2 , '2' ) into oracle_table ( id, code ) values( 3 , '3' ) into oracle_table ( id, code ) values( 4 , '4' ) select 1 from dual ; ``` 这种方式可以一次性插入多条记录,通过`insert all`指定要插入的表和值,最后使用`select 1 from dual`来触发插入操作[^1]。 2. **在Spring Boot + MyBatis项目中使用`foreach`标签**: ```xml <insert id="batchInsert"> insert into ZBXT_WD_ZKGZB ( RULE_ID, ZKBDM, ZKZDDM, ZKBMC, ZKZDMC, ZKGLB, ZKGLZD, ZKLX, BCLX, ZYZD, YXX, CREATE_TIME, CREATE_USER) values <foreach collection ="list" item="rule" separator="," > (#{rule.ruleId}, #{rule.zkbdm}, #{rule.zkzddm}, #{rule.zkbmc}, #{rule.zkzdmc}, #{rule.zkglb}, #{rule.zkglzd}, #{rule.zklx}, #{rule.bclx}, #{rule.zyzd}, #{rule.yxx}, #{rule.createTime}, #{rule.createUser}) </foreach> </insert> ``` 在MyBatis的Mapper文件中,使用`foreach`标签遍历要插入数据列表,将每条记录的字段值插入到指定表中[^3]。 3. **Spring Boot结合MyBatis - Plus使用`foreach`和`UNION ALL`**: ```xml <insert id="insertGC51"> insert into gc51( AGC156, AAC001, AGB020, AGE655, AAE100 ) <foreach collection="data" separator="UNION ALL" item="item" > select #{item.agc156,jdbcType=VARCHAR} agc156, #{item.aac001,jdbcType=VARCHAR} aac001, #{item.agb020,jdbcType=VARCHAR} agb020, #{item.age655,jdbcType=VARCHAR} age655, #{item.aae100,jdbcType=VARCHAR} aae100 from dual </foreach> </insert> ``` 同样在MyBatis的Mapper文件中,使用`foreach`标签遍历数据列表,通过`UNION ALL`将多个`select`语句组合起来,实现批量插入[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值