mysql存储过程编写
2019独角兽企业重金招聘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 = 1
mysql中,如果一个整数和另一个整数相除,得到的如果是一个有小数点的数字,即使把它赋给一个整数值,得到的还是一个小数,如下:
DECLARE temp_mobile_pre INT DEFAULT 0;
set temp_mobile_pre = 1395007/10000;
此时得到的值为会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
mysql存储过程编写相关推荐
- 软件测试mysql存储过程的用处实例_软件测试中实际应用:MySQL5存储过程编写
软件测试中实际应用:MySQL5存储过程编写 MySql5.0以后均支持存储过程,最近有空,研究了一把这个. 格式: CREATE PROCEDURE 过程名 ([过程参数[,...]]) [特性 . ...
- MySQL存储过程详解
1. 存储过程简介 我们常用的操作数据库语言SQL语句在执行的时候需要要先编译,然后执行,而存储过程(Stored Procedure)是一组为了完成特定功能的SQL语句集,经编译后存储 ...
- MySQL 存储过程初研究
最近在做一个移动设备多类型登录的统一用户系统.其中记录用户资料的部分,因为涉及到更换设备的相同用户.同一个用户多类型同时具备的情况,所以想分辨出尽量少的用户去合理记录,就需要多次查询.于是决定研究一下 ...
- mysql存储过程含义_MySQL存储过程定义中的特性(characteristic)的含义
MySQL的存储过程蛮啰嗦的,与MSSQL或者Oracle的存储过程相比,如果没有显式指定,他会隐含地指定一系列特性(characteristic)的默认值来创建存储过程 通常在使用图形界面工具进行存 ...
- MySQL存储过程详解 mysql 存储过程
mysql存储过程详解 1. 存储过程简介 我们常用的操作数据库语言SQL语句在执行的时候需要要先编译,然后执行,而存储过程(Stored Procedure)是一组为了完成特定功能的S ...
- mysql存储过程详解[转]
mysql存储过程详解[转] 1. 存储过程简介 我们常用的操作数据库语言SQL语句在执行的时候需要要先编译,然后执行,而存储过程(Stored Procedure)是一组为了完成特定功 ...
- mysql 动态传入表名 存储过程_面试再问MySQL存储过程和触发器就把这篇文章给他...
Mysql存储过程及触发器trigger 存储过程 一.一个简单的存储过程 1,一个简单的存储过程 delimiter $$create procedure testa()begin Select * ...
- mysql数据库存储数据的过程_[数据库]MySql存储过程总结
[数据库]MySql存储过程总结 0 2014-06-12 01:00:50 之前总是在MSSQL上写存储过程,没有在MYSQL上写过,也基本没有用过,今天需要用到MYSQL,研究了下,把项目的需要的 ...
- mysql存储过程中as_Mysql存储过程中的delimiter
这个命令与存储过程没什么关系吧. 其实就是告诉mysql解释器,该段命令是否已经结束了,mysql是否可以执行了. 默认情况下,delimiter是分号;.在命令行客户端中,如果有一行命令以分号结束, ...
最新文章
- Openstack部署工具
- 学生专用计算机如何打游戏,电竞专业学生日常:白天学语数外计算机,晚上才打游戏...
- Java中的内存划分
- SQLServer 系统数据库
- 天融信的FTP服务器
- 程序员!别再盲目刷视频了!坚持做这件事儿,AI 也难不倒你!
- LeetCode 36. Valid Sudoku
- Log4j2 Zero Day 漏洞 Apache Flink 应对指南(二)
- (一)、apache doris 介绍
- 美国大厂码农薪资曝光:年薪18万美元,够养家,不够买海景房
- python+selenium自动化软件测试(第1章)
- win7语言文件夹c盘什么位置,win7桌面上的文件 在c盘哪个目录
- 完美字符子串 单调队列预处理+DP线段树优化
- java 导出csv文件通过web下载
- 微信 SHA1 签名_微信公众号自动回复功能开发
- windows上获取系统时间
- 什么是GC,为什么需要GC?
- android之ListView布局
- cmd imp导入dmp文件_导入Oracle的dmp备份的dmp文件报错“IMP-00002:无法打开c:/Documents.DMP进行读取”...
- 涛思数据TDengine征稿 — 保姆级教程TDengine客户端服务器安装教程
热门文章
- TortoiseSVN无法查看日志和SVN LOG无法查看日志的解决办法。
- 最全的cisco ios下载,思科ios下载
- matplotlib - 极坐标上的散点图
- Angularjs1.x 项目结构
- Python——Django学习笔记
- HTML的相关路径与绝对路径的问题---通过网络搜索整理
- Fedora下用Iptux,中文乱码解决
- Spring(3.2.3) - Beans(11): depends-on
- Python编写自动化脚本(无验证码)
- 算法设计与分析python_Python算法设计与分析