儲存過程插入資料,網上的例子基本都是一些自動生成的,比如就改變主鍵id的值,其他資料都不變,這種能夠實現批量填充資料庫表的需求,但是如果是一批沒有規律的資料(生產環境資料匯入等)需要插入到資料庫,網上的例子就不太好用了。剛好公司有這個需求,寫了這個指令碼,發出來給大家做個參考:
`
create
orreplace
procedure format_store
ascomid integer
;formatid integer
;storeid integer
;entityid integer
;sourceid integer
;resourcepath varchar2(
255)
;type storet1 is
table
of varchar2(
255)
;type storet2 is
table
of varchar2(
255)
;type storet3 is
table
of varchar2(
255)
;type storet4 is
table
of varchar2(
255)
;storecode storet1 := storet1(
'qd0001'
,'qd0002'
,'qd0003'
,'qd0004'
,'qd0005'
,'qd0006'
,'qd0007'
,'qd0008'
,'qd0009'
,'qd0010'
,'qd0011'
,'qd0012'
,'qd0013'
,'qd0014'
,'qd0015'
,'qd0016'
,'qd0017'
,'qd0018'
,'qd0019');
storename1 storet2 := storet2(
'沙灘'
,'前廳部'
,'迷你吧'
,'洗衣房'
,'車隊'
,'花房'
,'房務精品店'
,'餐飲部'
,'運動中心'
,'娛樂中心大廳'
,'娛樂中心包房'
,'健康中心'
,'樂世界兒童王國'
,'溫泉湯屋'
,'啟東溫泉餐廳'
,'溫泉康樂部'
,'溫泉水療部'
,'前廳部'
,'迷你吧');
storename2 storet3 := storet3(
'海濱中心-沙灘'
,'房務部-前廳部'
,'房務部-迷你吧'
,'房務部-洗衣房'
,'房務部-車隊'
,'房務部-花房'
,'房務部-房務精品店'
,'餐飲部-餐飲部'
,'運動中心-運動中心'
,'娛樂中心-娛樂中心大廳'
,'娛樂中心-娛樂中心包房'
,'健康中心-健康中心'
,'兒童中心-樂世界兒童王國'
,'溫泉部-溫泉湯屋'
,'溫泉部-啟東溫泉餐廳'
,'溫泉部-溫泉康樂部'
,'溫泉部-溫泉水療部'
,'房務部(四星)-前廳部'
,'房務部(四星)-迷你吧');
formatcode storet4 := storet4(
'aaaa'
,'bbbbb'
,'ccccc'
,'ddddd'
,'eeeeee'
,'fffffff'
,'gggg'
,'hhhhhh'
,'jjjjjjj'
,'kkkkkkk'
,'!!!!!'
,'222222'
,'333333'
,'44444'
,'5555555'
,'66666'
,'dddd'
,'lyy1100107'
,'******xx');
begin
select entity_id into comid from table2 where id5 =
'341'
;for i in
1..storecode.count loop
insert
into
"table1"
("code"
,"name1"
,"name2"
,"id1"
,"code2"
,"phone"
,"tack"
,"id2"
,"code3"
,"name3"
,"version1"
,"data1"
,"by1"
,"by2"
,"data2"
,"id4"
)values
(storecode(i)
, storename1(i)
, storename2(i)
,'341'
,'y110',''
,'',null
, formatcode(i)
,null
,'1'
, to_timestamp(
'2020-06-22 20:11:51.958000'
,'syyyy-mm-dd hh24:mi:ss:ff6'),
'7',
'226'
, to_timestamp(
'2020-06-22 15:33:39.681000'
,'syyyy-mm-dd hh24:mi:ss:ff6'),
'2')
;select store_id into sourceid from table1 where code = storecode(i)
;insert
into table2(
"id5"
,"type1"
,"version1"
,"data1"
,"by1"
,"by2"
,"data2"
)values
(sourceid,
'store'
,'1'
, to_timestamp(
'2020-06-22 13:00:01.320000'
,'syyyy-mm-dd hh24:mi:ss:ff6'),
'7',
'226'
, to_timestamp(
'2020-06-22 13:00:00.707000'
,'syyyy-mm-dd hh24:mi:ss:ff6'))
;select id2 into formatid from table3 where code3 = formatcode(i)
;select entity_id into entityid from table2 where id5 = formatid and type1 =
'format'
;select entity_id into sourceid from table2 where id5 = sourceid and type1 =
'store'
;resourcepath := comid ||
'.'|| entityid ||
'.'|| sourceid;
update table2 set recursion_path = resourcepath,parent_id = entityid where entity_id = sourceid;
commit
;end
loop
;end;/
begin
format_store;
commit
;end;/
`
MySql批量插入資料 儲存過程
批量插入儲存過程 create procedure auto insert in indexs int,in loops int begin declare v sql longtext set v sql insert into t info name,time values while inde...
mysql 儲存過程 插入大量資料
需求 測試sql語句的效能,在資料庫中插入10萬條資料用於測試。delimiter drop procedure if exists kxc create procedure kxc begin declare i int set i 0 start transaction while i 1000...
mysql利用儲存過程批量插入資料
最近需要測試一下mysql單錶資料達到1000w條以上時增刪改查的效能。由於沒有現成的資料,因此自己構造,本文只是例項,以及簡單的介紹。首先當然是建表 create table fortest id int 30 unsigned not null auto increment primary ke...