MySQL存儲過程例子,包含事務,參數,嵌套調用,游標,循環等
MySQL存儲過程例子,包含事務,參數,嵌套調用,游標,循環等
drop procedure if exists pro_rep_shadow_rs;
delimiter |
----------------------------------
-- rep_shadow_rs
-- 用來處理信息的增加,更新和刪除
-- 每次只更新上次以來沒有做過的數據
-- 根據不同的標志位
-- 需要一個輸出的參數,
-- 如果返回為0,則調用失敗,事務回滾
-- 如果返回為1,調用成功,事務提交
--
-- 測試方法
-- call pro_rep_shadow_rs(@rtn);
-- select @rtn;
----------------------------------
create procedure pro_rep_shadow_rs(out rtn int)
begin
-- 聲明變量,所有的聲明必須在非聲明的語句前面
declare iLast_rep_sync_id int default -1;
declare iMax_rep_sync_id int default -1;
-- 如果出現異常,或自動處理並rollback,但不再通知調用方了
-- 如果希望應用獲得異常,需要將下面這一句,以及啟動事務和提交事務的語句全部去掉
declare exit handler for sqlexception rollback;
-- 查找上一次的
select eid into iLast_rep_sync_id from rep_de_proc_log where tbl='rep_shadow_rs';
-- 如果不存在,則增加一行
if iLast_rep_sync_id=-1 then
insert into rep_de_proc_log(rid,eid,tbl) values(0,0,'rep_shadow_rs');
set iLast_rep_sync_id = 0;
end if;
-- 下一個數字
set iLast_rep_sync_id=iLast_rep_sync_id+1;
-- 設置默認的返回值為0:失敗
set rtn=0;
-- 啟動事務
start transaction;
-- 查找最大編號
select max(rep_sync_id) into iMax_rep_sync_id from rep_shadow_rs;
-- 有新數據
if iMax_rep_sync_id>=iLast_rep_sync_id then
-- 調用
call pro_rep_shadow_rs_do(iLast_rep_sync_id,iMax_rep_sync_id);
-- 更新日志
update rep_de_proc_log set rid=iLast_rep_sync_id,eid=iMax_rep_sync_id where tbl='rep_shadow_rs';
end if;
-- 運行沒有異常,提交事務
commit;
-- 設置返回值為1
set rtn=1;
end;
|
delimiter ;
drop procedure if exists pro_rep_shadow_rs_do;
delimiter |
---------------------------------
-- 處理指定編號范圍內的數據
-- 需要輸入2個參數
-- last_rep_sync_id 是編號的最小值
-- max_rep_sync_id 是編號的最大值
-- 無返回值
---------------------------------
create procedure pro_rep_shadow_rs_do(last_rep_sync_id int, max_rep_sync_id int)
begin
declare iRep_operationtype varchar(1);
declare iRep_status varchar(1);
declare iRep_Sync_id int;
declare iId int;
-- 這個用於處理游標到達最後一行的情況
declare stop int default 0;
-- 聲明游標
declare cur cursor for select id,Rep_operationtype,iRep_status,rep_sync_id from rep_shadow_rs where rep_sync_id between last_rep_sync_id and max_rep_sync_id;
-- 聲明游標的異常處理,設置一個終止標記
declare CONTINUE HANDLER FOR SQLSTATE '02000' SET stop=1;
-- 打開游標
open cur;
-- 讀取一行數據到變量
fetch cur into iId,iRep_operationtype,iRep_status,iRep_Sync_id;
-- 這個就是判斷是否游標已經到達了最後
while stop <> 1 do
-- 各種判斷
if iRep_operationtype='I' then
insert into rs0811 (id,fnbm) select id,fnbm from rep_shadow_rs where rep_sync_id=iRep_sync_id;
elseif iRep_operationtype='U' then
begin
if iRep_status='A' then
insert into rs0811 (id,fnbm) select id,fnbm from rep_shadow_rs where rep_sync_id=iRep_sync_id;
elseif iRep_status='B' then
delete from rs0811 where id=iId;
end if;
end;
elseif iRep_operationtype='D' then
delete from rs0811 where id=iId;
end if;
-- 讀取下一行的數據
fetch cur into iId,iRep_operationtype,iRep_status,iRep_Sync_id;
end while; -- 循環結束
close cur; -- 關閉游標
end;
use testprocedure;
delimiter //
create procedure simpleproce1 (out par1 int)
begin
select count(*) into par1 from proce;
end
//
delimiter ;
call simpleproce1(@a);
select @a;
#<2>,每次只有單一的行可以被取回select id,name into par1,par2 from proce LIMIT 1;中的LIMIT 1;
use testprocedure;
delimiter //
DROP procedure IF EXISTS simpleproce2
create procedure simpleproce2 (out par1 int,out par2 char(30))
begin
select id,name into par1,par2 from proce LIMIT 1;
end
//
delimiter ;
call simpleproce2(@a,@b);
select @a,@b;
## *********second test,function************
#<3>
delimiter //
DROP FUNCTION IF EXISTS hello
//
create function hello(s char(20)) returns char(50)
return concat('Hello, ',s,'!');
//
delimiter ;
select hello('world');
show create function testprocedure.helloG
#它返回子程序的特征,如數據庫,名字,類型,創建者及創建和修改日期
show function status like 'hello'G
#<4>
#注意name不能和字段名相同
delimiter //
DROP procedure IF EXISTS test //
CREATE PROCEDURE test ()
BEGIN
DECLARE name VARCHAR(5) DEFAULT 'bob';
DECLARE newname VARCHAR(5);
DECLARE xid INT;
SELECT name,id INTO newname,xid
FROM proce WHERE name = name;
SELECT newname;
END;
//
call test1() //
#***
delimiter //
DROP procedure IF EXISTS test2 //
CREATE PROCEDURE test2 ()
BEGIN
DECLARE newname VARCHAR(5);
DECLARE xid INT;
SELECT name,id INTO newname,xid
FROM proce limit 1;
SELECT newname,xid;
END;
//
call test2() //
#<5>
use testprocedure;
CREATE PROCEDURE p1 () SELECT * FROM proce;
call p1();
#<6>注意此處的handler是設置SQLSTATE值,SQLWARNING是對所有以01開頭的SQLSTATE代碼的速記
#NOT FOUND是對所有以02開頭的SQLSTATE代碼的速記
#SQLEXCEPTION是對所有沒有被SQLWARNING或NOT FOUND捕獲的SQLSTATE代碼的速記
#DECLARE CONTINUE HANDLER聲明CONTINUE異常處理
#事實上這裡的23000SQLSTATE是更常用的,當外鍵約束出錯或主鍵約束出錯就被調用了。
#當沒有發生該23000異常時, select @x2的值將是null,而不是1,
#並且後面的第2個語句執行時將會報主鍵約束錯誤,此時@x2=1,@x=4,雖然第2句有了異常,但是後面的語句繼續執行
#保存到數據的數據是3,test3和5,test5
use testprocedure;
delimiter //
DROP procedure IF EXISTS handlerdemo
//
create procedure handlerdemo()
begin
declare continue handler for sqlstate '23000' set @x2=1;
set @x=1;
insert into proce values(3,'test3');
set @x=2;
insert into proce values(3,'test4');
set @x=3;
insert into proce values(5,'test5');
set @x=4;
end;
//
call handlerdemo()//
select @x //
select @x2 //
## ************光標****************
#<7>光標必須在聲明處理程序之前被聲明,並且變量和條件必須在聲明光標或處理程序之前被聲明
#在這裡先聲明變量a,b,c,後聲明cursor
create procedure curdemo()
begin
declare done int default 0;
declare a char(16);
declare b,c int;
declare cur1 cursor for select id,name from proce;
declare cur2 cursor for select id from proce2;
declare continue handler for sqlstate '02000' set done=1;
open cur1;
open cur2;
repeat
fetch cur1 into b,a;
fetch cur2 into c;
if not done then
if b<c then
insert into proce3 values(b,a);
else
insert into proce3 values(c,a);
end if;
end if;
until done end repeat;
close cur1;
close cur2;
end
## **************** Case *******************
#<8>when ... then ;case ... end case;
delimiter //
DROP procedure IF EXISTS p13
//
create procedure p13(in par1 int)
begin
declare var1 int;
set var1=par1+1;
case var1
when 0 then insert into casetest values(17);
when 1 then insert into casetest values(18);
else insert into casetest values(19);
end case;
end;
//
call p13(-1)//
call p13(0)//
call p13(1)//
call p13(null)//
## **************** while ****************
#<9>while ... do ... end while;為了防止null的錯誤,set v=0是必須的
delimiter //
DROP procedure IF EXISTS p14
//
create procedure p14()
begin
declare v int;
set v=0;
while v < 5 do
insert into casetest values (v);
set v=v+1;
end while;
end;//
call p14()//
## ***************** repeat *****************
#<10>repeat ...until ... end repeat; 是執行後檢查(until v>=5),而while是執行前檢查(while v<5)
delimiter //
DROP procedure IF EXISTS p15
//
create procedure p15()
begin
declare v int;
set v=0;
repeat
insert into casetest values(v);
set v=v+1;
until v >=5
end repeat;
end;
//
call p15()//
## ***************** loops *****************
#<11> loop 和while一樣不需要初始條件,同時和repeat一樣不需要結束條件
# loop_label: loop
# ...
# if .. then
# leave loop_label
# end if
# end loop
delimiter //
DROP procedure IF EXISTS p16
//
create procedure p16()
begin
declare v int;
set v=0;
loop_label: loop
insert into casetest values(v);
set v=v+1;
if v >=5 then
leave loop_label;
end if;
end loop;
end;//
call p16()//
## ***************** Labels *****************
# <12>labels標號; 注意此處的until 0=0後面沒有分號“;”
delimiter //
DROP procedure IF EXISTS p17//
create procedure p17()
label_1:begin
label_2:while 0=1 do leave label_2; end while;
label_3:repeat leave label_3;until 0=0 end repeat;
label_4:loop leave label_4; end loop;
end;//
call p17()//
#<13>labels 標號結束符;
delimiter //
DROP procedure IF EXISTS p18//
create procedure p18()
label_1:begin
label_2:while 0=1 do leave label_2; end while label_2;
label_3:repeat leave label_3;until 0=0 end repeat label_3;
label_4:loop leave label_4; end loop label_4;
end label_1;//
call p18()//
#<14>leave和labels 跳出和標號;leave 使程序跳出復雜的語句
delimiter //
DROP procedure IF EXISTS p19//
create procedure p19(par char)
label_1:begin
label_2:begin
label_3:begin
if par is not null then
if par='a' then leave label_1;
else
begin
if par='b' then
leave label_2;
else
leave label_3;
end if;
end;
end if;
end if;
end label_3;
end label_2;
end label_1;
//
call p19('a')//
#<15>iterate迭代,必須用leave;iterate意思是重新開始復合語句,相當於 continue
#該結果中3將不被保存到數據庫表中
delimiter //
DROP procedure IF EXISTS p20//
create procedure p20()
begin
declare v int;
set v=0;
loop_label:loop
if v=3 then
set v=v+1;
iterate loop_label;
end if;
insert into casetest values(v);
set v=v+1;
if v>=5 then
leave loop_label;
end if;
end loop loop_label;
end;//
call p20()//
#<16>Grand combination大組合
delimiter //
DROP procedure IF EXISTS p21//
create procedure p21(in par1 int,out par2 int)
language sql deterministic sql security invoker
begin
declare v int;
label goto_label;
start_label:loop
if v=v then
leave start_label;
else
iterate start_label;
end if;
end loop start_label;
repeat
while 1=0 do begin end;
end while;
until v=v
end repeat;
goto goto_label;
end;
//
call p21()//
## **************** trigger ***************************
#<17>
use testprocedure;
CREATE TABLE trig1(a1 int);
CREATE TABLE trig2(a2 int);
CREATE TABLE trig3(a3 int not null AUTO_INCREMENT PRIMARY KEY);
CREATE TABLE trig4(
a4 INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
b4 INT DEFAULT 0
);
insert into trig3(a3) values(null),(null),(null),(null),(null),(null),(null),(null),(null),(null);
insert into trig4(a4) values(0),(0),(0),(0),(0),(0),(0),(0),(0),(0);
delimiter //
DROP trigger trigtest//
create trigger trigtest before insert on trig1
for each row begin
insert into trig2 set a2=NEW.a1;
delete from trig3 where a3=NEW.a1;
update trig4 set b4=b4+1 where a4=NEW.a1;
end;
//
delimiter ;
INSERT INTO trig1 VALUES(1), (3), (1), (7), (1), (8), (4), (4);