記錄一下本身寫的一個mysql存儲過程,在遊標方面和oracle有些不同,mysql是使用一個HANDLER來處理數據讀取完的狀況,以下:mysql
DECLARE CONTINUE HANDLER FOR NOT FOUND SET no_more_mobilearea = 1;
當沒有數據時,把no_more_mobilearea這個變量設置爲1,以下:sql
FETCH cur_dm_mobile INTO temp_id, temp_mobile, temp_province_name, temp_city_name; UNTIL no_more_mobilearea = 1
mysql中,若是一個整數和另外一個整數相除,獲得的若是是一個有小數點的數字,即便把它賦給一個整數值,獲得的仍是一個小數,以下:
數據庫
DECLARE temp_mobile_pre INT DEFAULT 0; set temp_mobile_pre = 1395007/10000;
此時獲得的值爲會139.5007,即便把它賦給一個整數變量也沒有,若是想獲得一個整數值,能夠用FLOOR(向下取整)或ceil(向上取整)或round(四捨五入),如:api
set temp_mobile_pre = FLOOR(temp_mobile/10000);
獲得就是139了oracle
mysql中的查找某個字符在字符串的函數:LOCATE(substr, str),注意是要查找的字符串在前面,後面是被查找的字符串,我開始弄錯了。函數
mysql中的截取字符串的函數:substring(str, pos),left等,注意substring好象不支持從某個位置開始截取指定的長度,雖然給出的api說是能夠,但我實際試了不行,可能跟個人數據庫版本有關係,只能用left函數spa
mysql中鏈接兩個字符串:concatcode
完整的一個存儲過程以下:ci
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; REPEAT set 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) THEN set 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) THEN set 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) THEN set temp_type = 3; ELSEIF (temp_mobile_pre = 145 || temp_mobile_pre = 147) THEN set temp_type = 4; ELSE SET 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) THEN select territorycode into temp_province_code from t_s_territory where territoryname like concat(temp_province_name, '%'); ELSEIF (temp_count > 1) THEN select territorycode into temp_province_code from t_s_territory where territoryname like concat(temp_province_name, '%') limit 1; ELSE set 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) THEN select territorycode into temp_city_code from t_s_territory where territoryname like concat(temp_city_name, '%'); ELSEIF (temp_count > 1) THEN select territorycode into temp_city_code from t_s_territory where territoryname like concat(temp_city_name, '%') limit 1; ELSE set temp_city_code = 'null'; end if; if (temp_province_code <> 'null') THEN IF (temp_city_code = 'null') THEN set 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) then set 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