mysql存储过程编写
2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>
記錄一下自己寫(xiě)的一個(gè)mysql存儲(chǔ)過(guò)程,在游標(biāo)方面和oracle有些不一樣,mysql是使用一個(gè)HANDLER來(lái)處理數(shù)據(jù)讀取完的情況,如下:
DECLARE??CONTINUE?HANDLER?FOR?NOT?FOUND??SET??no_more_mobilearea?=?1;當(dāng)沒(méi)有數(shù)據(jù)時(shí),把no_more_mobilearea這個(gè)變量設(shè)置為1,如下:
FETCH??cur_dm_mobile?INTO?temp_id,?temp_mobile,?temp_province_name,?temp_city_name; UNTIL??no_more_mobilearea?=?1mysql中,如果一個(gè)整數(shù)和另一個(gè)整數(shù)相除,得到的如果是一個(gè)有小數(shù)點(diǎn)的數(shù)字,即使把它賦給一個(gè)整數(shù)值,得到的還是一個(gè)小數(shù),如下:
此時(shí)得到的值為會(huì)139.5007,即使把它賦給一個(gè)整數(shù)變量也沒(méi)有,如果想得到一個(gè)整數(shù)值,可以用FLOOR(向下取整)或ceil(向上取整)或round(四舍五入),如:
set?temp_mobile_pre?=?FLOOR(temp_mobile/10000);得到就是139了
mysql中的查找某個(gè)字符在字符串的函數(shù):LOCATE(substr, str),注意是要查找的字符串在前面,后面是被查找的字符串,我開(kāi)始弄錯(cuò)了。
mysql中的截取字符串的函數(shù):substring(str, pos),left等,注意substring好象不支持從某個(gè)位置開(kāi)始截取指定的長(zhǎng)度,雖然給出的api說(shuō)是可以,但我實(shí)際試了不行,可能跟我的數(shù)據(jù)庫(kù)版本有關(guān)系,只能用left函數(shù)
mysql中連接兩個(gè)字符串:concat
完整的一個(gè)存儲(chǔ)過(guò)程如下:
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轉(zhuǎn)載于:https://my.oschina.net/u/914897/blog/401277
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的mysql存储过程编写的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 天穹游侠怎么得
- 下一篇: MySQL多实例实现半同步复制