《Mysql必讀mysql存儲過程中使用事務實例》要點:
本文介紹了Mysql必讀mysql存儲過程中使用事務實例,希望對您有用。如果有疑問,可以聯系我們。
例子,mysql存儲過程中使用事務.
?MYSQL入門
create definer=`root`@`localhost` procedure `createbusiness`(parameter1 int)
begin
??? #routine body goes here...
??? declare flag int default parameter1;#聲明變量flag,將參數值賦給該變量
??? declare uuidstr varchar(32);#聲明一個長度為32位的字符串
??? declare currenttime timestamp;#聲明一個類型為時間戳的變量
??? declare err int default 0;#聲明一個整形變量err,默認值是0
??? declare continue handler for sqlexception set err=1;#當sqlexception handler捕捉到異常時,設置err=1MYSQL入門
??? start transaction;#開始事務
??? while flag>0 do #注意: while不能空實現(在while塊中,里面必須有語句)
??????? #uuid()函數得到的字符串是'6ccd780c-baba-1026-9564-0040f4311e29',剔除里面的-,得到一個32位的字符串
??????? set uuidstr = replace(uuid(),'-','') ;
??????? #得到當前的時間
??????? set currenttime = current_timestamp();
??????? #執行插入語句,注意連接字符串的函數concat(str1,str2,...);其中str..也可以是數字類型
??????? insert into
????????? 表名稱
?????????? (id,title,keyword,hasimage,istodayhead,isshowinhome,isbigness,publishtime,originid,modify_time,isanalysis)
???????? value
???????? (uuidstr,concat('事件標題',flag),concat('關鍵字',flag),1,1,0,0,currenttime,concat('xxxxxxx',flag),currenttime,1);
??????? #每循環一次,flag要減去1,注意沒有flag--的語法
??????? set flag = flag-1;
??????? #在這里測試當err=1時,事務是否有了回滾,測試ok
??????? #if flag=7 then?? #注意在procedure中給變量賦值要用到set,或在變量聲明時用default來父子,所以=號可以用來比較兩邊的值是否相等,<=>也可,區別先不去糾結.
??????????????? #set err=1;
??????? #end if;
??? end while;MYSQL入門
??? if (err=0) then
??????? commit;
??????? select 'ok';
???? else
??????? rollback;
??????? select 'err';
???? end if;
end;MYSQL入門
轉載請注明本頁網址:
http://www.snjht.com/jiaocheng/6451.html