mysql儲存過程小例子

2021-07-31 13:06:29 字數 1918 閱讀 2174

drop procedure if exists customview_logs_pro;

delimiter //

create procedure customview_logs_pro(in counts bigint)

begin

declare i bigint default 1;

declare total_count int default 1;

declare timestr varchar(19);

declare from_source_id varchar(64);

declare daytime bigint;

declare longtime bigint default 0;

declare answer_rand int;

declare answer_id int;

declare answer_name varchar(64);

declare input_model int;

declare input_rand int;

while i<=counts do

set daytime = (select round(rand()*30)) + 1;

set timestr=concat('2017-04-',daytime,' 20:00:00');

set longtime=longtime+i;

set answer_rand = (select round(rand()*5)) + 1;

set input_rand = (select round(rand()*4)) + 1;

if input_rand=1

then

set input_model = 0;

else

set input_model = 1;

end if;

if answer_rand=1

then

set answer_id = 1;

set answer_name = 'xiaoxiao';

elseif answer_rand=2

then

set answer_id = 3;

set answer_name = 'hasika';

elseif answer_rand=3

then

set answer_id = 4;

set answer_name = 'tianlalu';

elseif answer_rand=4

then

set answer_id = 6;

set answer_name = 'shuren';

else

set answer_id = 8;

set answer_name = 'lianjin';

end if;

set from_source_id=concat('userid_',(select round(rand()*1000000)),'_',longtime);

insert into table_name(id,from_source_id) values(i,191585688,2,from_source_id);

if mod(i,10000)=0 then

commit;

end if;

set i = i + 1;

end while;

commit;

endcall service_logs_pro(10000000);

需要注意的是:

如果不需要將插入千萬級資料的操作寫在乙個事務中,那麼就需要如上所示,每隔10000條資料提交一次,否則會報「事務日誌已滿」的錯誤,導致無法完成插入操作;

如果需要寫在乙個事務中,那麼可以考慮將事務日誌的空間擴充套件。

mysql 儲存過程 例子 MySQL儲存過程例子

索引 index create index idx sname on student sname 4 alter table teacher add index idx tname tname drop index idx sname on student 檢視 view create view v...

mysql帶參儲存過程小例子

儲存過程p get class name是根據輸入的班級號判斷班級名稱 儲存過程p insert student是接收輸入的學生資訊,最終將資訊插入學生表。sql view plain copy print?drop procedure if exists p get class name crea...

mysql 儲存過程例子

size large b 1 迴圈遍歷值 b size drop procedure if exists doit delimiter表示以 結束編譯 delimiter create procedure doit in parameter integer begin declare v1 int ...