oracle存储过程怎样批量插入新数据

oracle存储过程怎样批量插入新数据,第1张

需要生成的SQL

insert into TMP_UPSTATE_CASEKEY values('TMP0000001', 1, sysdate)

存储过程实现

create or replace procedure proc_casekey_upstate

as

casekey char(14)

begin

for i in 1..10000000 loop

casekey := 'TMP'||lpad(i,7,0)-- TMP0000001

insert into TMP_UPSTATE_CASEKEY values(casekey, 1, sysdate)

end loop

commit

end

begin

proc_casekey_upstate()

end

测试发现生成一千万条数据用了14分钟左右,性能还是可以了,如果先去掉TMP_NUM_STATUS_ID的外键估计更快。

或者:

insert into TMP_UPSTATE_CASEKEY select 'TMP'||LPAD(rownum,7,0),1,sysdate from dual connect by level <= 1000000

用循环语句生成字段值insert

SQL 如下:

create procedure InsertTest

as

begin

declare @i int

declare @s varchar(20)

set @i=1

while @i<=100

begin

select @s=cast(@i AS varchar(20))

insert into test (A,B,C,D) VALUES ('a'+@s,'b'+@s,'ccccc','ddddd')

SET @i=@i+1

end

end

一下代码运行通过:

delimiter $$

create procedure lucia_proc16(count int)

begin

DECLARE name_proc VARCHAR(20) CHARACTER SET utf8

DECLARE sex_proc VARCHAR(4) CHARACTER SET utf8

DECLARE age_proc INT(10)

DECLARE class_proc VARCHAR(20) CHARACTER SET utf8

DECLARE Addr_proc VARCHAR(50) CHARACTER SET utf8

DECLARE i INT

set i = 1

set sex_proc = '女'

set age_proc = 20

set class_proc = '山治班'

set Addr_proc = '北京市朝阳区'

while i<count do

set name_proc = CONCAT('露西亚',i)

insert into students(Name,Sex,age,class,Addr)values(name_proc,sex_proc,age_proc,class_proc,Addr_proc)

set i = i+1

end while

end

$$

delimiter

代码功能:

传入一个行数,控制插入多少条数据

运行效果:


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/11926121.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-19
下一篇 2023-05-19

发表评论

登录后才能评论

评论列表(0条)

保存