mysql存储过程编写
生活随笔
收集整理的這篇文章主要介紹了
mysql存储过程编写
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
2019獨角獸企業(yè)重金招聘Python工程師標準>>>
記錄一下自己寫的一個mysql存儲過程,在游標方面和oracle有些不一樣,mysql是使用一個HANDLER來處理數據讀取完的情況,如下:
DECLARE??CONTINUE?HANDLER?FOR?NOT?FOUND??SET??no_more_mobilearea?=?1;當沒有數據時,把no_more_mobilearea這個變量設置為1,如下:
FETCH??cur_dm_mobile?INTO?temp_id,?temp_mobile,?temp_province_name,?temp_city_name; UNTIL??no_more_mobilearea?=?1mysql中,如果一個整數和另一個整數相除,得到的如果是一個有小數點的數字,即使把它賦給一個整數值,得到的還是一個小數,如下:
此時得到的值為會139.5007,即使把它賦給一個整數變量也沒有,如果想得到一個整數值,可以用FLOOR(向下取整)或ceil(向上取整)或round(四舍五入),如:
set?temp_mobile_pre?=?FLOOR(temp_mobile/10000);得到就是139了
mysql中的查找某個字符在字符串的函數:LOCATE(substr, str),注意是要查找的字符串在前面,后面是被查找的字符串,我開始弄錯了。
mysql中的截取字符串的函數:substring(str, pos),left等,注意substring好象不支持從某個位置開始截取指定的長度,雖然給出的api說是可以,但我實際試了不行,可能跟我的數據庫版本有關系,只能用left函數
mysql中連接兩個字符串:concat
完整的一個存儲過程如下:
CREATE?DEFINER?=?'root'@'localhost' PROCEDURE?ddo.test2() BEGIN#Routine?body?goes?here... DECLARE??no_more_mobilearea,?temp_idx,?temp_nums,?temp_type,?temp_mobile_pre,?temp_count?INT?DEFAULT?0; DECLARE??temp_id?VARCHAR(32); DECLARE??temp_province_name,?temp_city_name?VARCHAR(50); DECLARE??temp_province_code,?temp_city_code?VARCHAR(10); DECLARE?temp_mobile?NUMERIC(7); DECLARE??cur_dm_mobile?CURSOR?FOR?SELECT?id,?mobilenumber,?province_name,?city_name?FROM?dm_mobile; #DECLARE??cur_dm_mobile?CURSOR?FOR?SELECT?id,?mobilenumber,?province_name,?city_name?FROM?dm_mobile?WHERE?province_code?IS?null; #DECLARE??cur_dm_mobile?CURSOR?FOR?SELECT?id,?mobilenumber,?province_name,?city_name?FROM?dm_mobile?WHERE?id?=?'285001'; DECLARE??CONTINUE?HANDLER?FOR?NOT?FOUND??SET??no_more_mobilearea?=?1; OPEN??cur_dm_mobile; FETCH??cur_dm_mobile?INTO?temp_id,?temp_mobile,?temp_province_name,?temp_city_name; REPEATset?temp_mobile_pre?=?FLOOR(temp_mobile/10000);if?(temp_mobile_pre?=?133?||?temp_mobile_pre?=?153?||?temp_mobile_pre?=?180?||?temp_mobile_pre?=?189?||?temp_mobile_pre?=?181?||?temp_mobile_pre?=?170||?temp_mobile_pre?=?177)?THENset?temp_type?=?1;ELSEIF?(temp_mobile_pre?=?134?||?temp_mobile_pre?=?135?||?temp_mobile_pre?=?136?||?temp_mobile_pre?=?137?||?temp_mobile_pre?=?138?||?temp_mobile_pre?=?139||?temp_mobile_pre?=?150?||?temp_mobile_pre?=?151?||?temp_mobile_pre?=?152?||?temp_mobile_pre?=?157?||?temp_mobile_pre?=?158?||?temp_mobile_pre?=?159?||?temp_mobile_pre?=?182?||?temp_mobile_pre?=?183?||?temp_mobile_pre?=?184?||?temp_mobile_pre?=?187?||?temp_mobile_pre?=?188)?THENset?temp_type?=?2;ELSEIF?(temp_mobile_pre?=?130?||?temp_mobile_pre?=?131?||?temp_mobile_pre?=?132?||?temp_mobile_pre?=?155?||?temp_mobile_pre?=?156?||?temp_mobile_pre?=?185||?temp_mobile_pre?=?186?||?temp_mobile_pre?=?176)?THENset?temp_type?=?3;ELSEIF?(temp_mobile_pre?=?145?||?temp_mobile_pre?=?147)?THENset?temp_type?=?4;ELSESET?temp_type?=?5; end?if;select?count(*)?into?temp_count?from?t_s_territory?where?territoryname?like?concat(temp_province_name,?'%');if?(temp_count?=?1)?THENselect?territorycode?into?temp_province_code?from?t_s_territory?where?territoryname?like?concat(temp_province_name,?'%');ELSEIF?(temp_count?>?1)?THENselect?territorycode?into?temp_province_code?from?t_s_territory?where?territoryname?like?concat(temp_province_name,?'%')?limit?1;ELSEset?temp_province_code?=?'null';end?if;select?count(*)?into?temp_count?from?t_s_territory?where?territoryname?like?concat(temp_city_name,?'%');if?(temp_count?=?1)?THENselect?territorycode?into?temp_city_code?from?t_s_territory?where?territoryname?like?concat(temp_city_name,?'%');ELSEIF?(temp_count?>?1)?THENselect?territorycode?into?temp_city_code?from?t_s_territory?where?territoryname?like?concat(temp_city_name,?'%')?limit?1;ELSEset?temp_city_code?=?'null';end?if; if?(temp_province_code?<>?'null')?THENIF?(temp_city_code?=?'null')?THENset?temp_city_code?=?temp_province_code;END?IF;update?dm_mobile?set?province_code?=?temp_province_code,?city_code?=?temp_city_code,?type?=?temp_type?where?id?=?temp_id; end?if;if?(temp_nums?=?1000)?thenset?temp_nums?=?0;commit;end?if; FETCH??cur_dm_mobile?INTO?temp_id,?temp_mobile,?temp_province_name,?temp_city_name; UNTIL??no_more_mobilearea?=?1 end?REPEAT; CLOSE??cur_dm_mobile; commit; END轉載于:https://my.oschina.net/u/914897/blog/401277
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的mysql存储过程编写的全部內容,希望文章能夠幫你解決所遇到的問題。