mysqlストレージ・プロシージャの作成


自分で書いた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 = 1

mysqlでは、1つの整数と別の整数を除いて、小数点のある数字であれば、整数値に与えても小数点で得られます.以下のようになります.
DECLARE  temp_mobile_pre INT DEFAULT 0;
set temp_mobile_pre = 1395007/10000;

このとき得られる値は139.507であり、整数変数に与えても整数値が得られない.整数値を得るには、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には2つの文字列が接続されています: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;
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