儲存過程寫法

2022-03-28 05:25:21 字數 3506 閱讀 5088

引用:

儲存過程呼叫:

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; 

儲存過程寫法

引用 儲存過程呼叫 drop procedure if exists pro rep shadow rs delimiter rep shadow rs 用來處理資訊的增加,更新和刪除 每次只更新上次以來沒有做過的資料 根據不同的標誌位 需要乙個輸出的引數,如果返回為0,則呼叫失敗,事務回滾 如果返...

儲存過程寫法

建立儲存過程執行刪除操作 alter procedure dbo ad preempted timer asdeclare pid varchar 32 declare times date declare nowtime date declare difftime int 定義乙個游標 decla...

儲存過程幾種寫法

1 建立使用引數的儲存過程 create proc au info lastname varchar 40 firstname varchar 20 asselect au lname,au fname,title,pub name from where au fname firstname and...