欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

MySQL存儲過程例子(包含事務(wù),輸出參數(shù),嵌套調(diào)用)

 更新時間:2010年09月13日 00:00:07   作者:  
MySQL存儲過程例子,包含事務(wù),輸出參數(shù),嵌套調(diào)用,學(xué)習(xí)mysql存儲過程的朋友可以參考下。
drop procedure if exists pro_rep_shadow_rs;
delimiter |
----------------------------------
-- rep_shadow_rs
-- 用來處理信息的增加,更新和刪除
-- 每次只更新上次以來沒有做過的數(shù)據(jù)
-- 根據(jù)不同的標志位
-- 需要一個輸出的參數(shù),
-- 如果返回為0,則調(diào)用失敗,事務(wù)回滾
-- 如果返回為1,調(diào)用成功,事務(wù)提交
--
-- 測試方法
-- 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;

-- 如果出現(xiàn)異常,或自動處理并rollback,但不再通知調(diào)用方了
-- 如果希望應(yīng)用獲得異常,需要將下面這一句,以及啟動事務(wù)和提交事務(wù)的語句全部去掉
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;

-- 下一個數(shù)字
set iLast_rep_sync_id=iLast_rep_sync_id+1;

-- 設(shè)置默認的返回值為0:失敗
set rtn=0;

-- 啟動事務(wù)
start transaction;

-- 查找最大編號
select max(rep_sync_id) into iMax_rep_sync_id from rep_shadow_rs;
-- 有新數(shù)據(jù)
if iMax_rep_sync_id>=iLast_rep_sync_id then
-- 調(diào)用
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;

-- 運行沒有異常,提交事務(wù)
commit;
-- 設(shè)置返回值為1
set rtn=1;
end;


delimiter ;
drop procedure if exists pro_rep_shadow_rs_do;

delimiter |
---------------------------------
-- 處理指定編號范圍內(nèi)的數(shù)據(jù)
-- 需要輸入2個參數(shù)
-- 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;
-- 聲明游標的異常處理,設(shè)置一個終止標記
declare CONTINUE HANDLER FOR SQLSTATE '02000' SET stop=1;

-- 打開游標
open cur;

-- 讀取一行數(shù)據(jù)到變量
fetch cur into iId,iRep_operationtype,iRep_status,iRep_Sync_id;
-- 這個就是判斷是否游標已經(jīng)到達了最后
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;

-- 讀取下一行的數(shù)據(jù)
fetch cur into iId,iRep_operationtype,iRep_status,iRep_Sync_id;

end while; -- 循環(huán)結(jié)束
close cur; -- 關(guān)閉游標
end;

相關(guān)文章

  • Mysql8.0輕松實現(xiàn)主從復(fù)制

    Mysql8.0輕松實現(xiàn)主從復(fù)制

    這篇文章主要介紹了Mysql8.0輕松實現(xiàn)主從復(fù)制方法的相關(guān)資料,需要的朋友可以參考下
    2022-11-11
  • 用SQL實現(xiàn)統(tǒng)計報表中的"小計"與"合計"的方法詳解

    用SQL實現(xiàn)統(tǒng)計報表中的"小計"與"合計"的方法詳解

    本篇文章是對使用SQL實現(xiàn)統(tǒng)計報表中的"小計"與"合計"的方法進行了詳細的分析介紹,需要的朋友參考下
    2013-06-06
  • 解決MySQL登錄報錯1045-Access?denied?for?user?'root'@' '(using?password:YES)

    解決MySQL登錄報錯1045-Access?denied?for?user?'root'@

    這篇文章主要給大家介紹了關(guān)于解決MySQL登錄報錯1045-Access?denied?for?user?‘root‘@‘‘(using?password:YES)的相關(guān)資料,文中一步步將解決的辦法介紹的非常詳細,需要的朋友可以參考下
    2023-07-07
  • MySQL中的最左匹配原則

    MySQL中的最左匹配原則

    這篇文章主要介紹了MySQL中的最左匹配原則,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • 你一定用的上的MySQL批量插入技巧分享

    你一定用的上的MySQL批量插入技巧分享

    MySQL一直是我們互聯(lián)網(wǎng)行業(yè)比較常用的數(shù)據(jù),我們也常常需要使用半ORM框架進行MySQL大批量插入操作,下面小編為大家整理了一些MySQL批量插入技巧,希望對大家有所幫助
    2024-01-01
  • mysql5.7.21.zip安裝教程

    mysql5.7.21.zip安裝教程

    這篇文章主要為大家詳細介紹了mysql5.7.21.zip安裝教程,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-02-02
  • 你還在 Select * 嗎?

    你還在 Select * 嗎?

    這篇文章主要介紹了MySql Select ,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • mysql數(shù)據(jù)庫中的索引類型和原理解讀

    mysql數(shù)據(jù)庫中的索引類型和原理解讀

    這篇文章主要介紹了mysql數(shù)據(jù)庫中的索引類型和原理,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • mysql8 公用表表達式CTE的使用方法實例分析

    mysql8 公用表表達式CTE的使用方法實例分析

    這篇文章主要介紹了mysql8 公用表表達式CTE的使用方法,結(jié)合實例形式分析了mysql8 公用表表達式CTE的基本功能、原理使用方法及相關(guān)操作注意事項,需要的朋友可以參考下
    2020-02-02
  • mysql查看表結(jié)構(gòu)的三種方法總結(jié)

    mysql查看表結(jié)構(gòu)的三種方法總結(jié)

    這篇文章主要介紹了mysql查看表結(jié)構(gòu)的三種方法總結(jié),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-07-07

最新評論