一、存储过程的创建
语法:
CREATE PROCEDURE sp_name (参数)
合法的SQL语句
mysql> delimiter // mysql> CREATE PROCEDURE simpleproc (OUT param1 INT) -> BEGIN -> SELECT COUNT(*) INTO param1 FROM example_members; -> END -> // Query OK, 0 rows affected (0.00 sec) mysql> delimiter ; mysql> CALL simpleproc(@a); Query OK, 0 rows affected (0.00 sec)
mysql> select @a//
+------+
| @a |
+------+
| 8 |
+------+
1 row in set (0.01 sec)
二、输入输出
IN/OUT 变量名 类型
CREATE PROCEDURE testproc(IN p_in int)
CREATE PROCEDURE testproc(OUT p_out int)
三、变量
变量的定义:DECLARE 变量名 type
DECLARE l_int int unsigned default 4000000;
DECLARE l_numeric number(8,2) DEFAULT 9.95;
DECLARE l_date date DEFAULT '1999-12-31';
变量的赋值:
SET 变量名 = 值
四、注释
单行注释:--
多行注释:/* */
五、存储过程的使用
调用:call procedurename(参数)
查询:show procedure status where db='dbname'
修改:ALTER PROCEDURE sp_name [characteristic ...]
删除:DROP PROCEDURE IF EXISTS simpleproc;
六、存储过程控制语句
条件语句:
if ,then, else, end if
create procedure test(in a int) if a > 1 then select 1; elseif a>2 then select 2; else -- do nothing -- set @tmp=1; end if;
case, when
SELECT case gender WHEN 1 THEN 'male' WHEN 0 THEN 'female' end as gender FROM student
三种循环:
while , do , end while
WHILE x <= 5 DO SET str = CONCAT(str,x,','); SET x = x + 1; END WHILE;
repeat end repeat
CREATE PROCEDURE p_repeat() BEGIN DECLARE v INT; SET v = 0; REPEAT INSERT INTO t VALUES(v); SET v = v + 1; UNTIL v>=5 END REPEAT; END;//
loop end loop
CREATE PROCEDURE p_loop() BEGIN DECLARE v INT; SET v = 0; loop_label:LOOP INSERT INTO t VALUES(v); SET v = v + 1; IF v>=5 THEN LEAVE loop_label; END IF; END LOOP; END;//
七、存储过程的常用函数
① 字符串类
CHARSET(str) //返回字串字符集
CONCAT (string2 [,... ]) //连接字串
INSTR (string ,substring ) //返回substring首次在string中出现的位置,不存在返回0
LCASE (string2 ) //转换成小写
LEFT (string2 ,length ) //从string2中的左边起取length个字符
LENGTH (string ) //string长度
LOAD_FILE (file_name ) //从文件读取内容
LOCATE (substring , string [,start_position ] ) 同INSTR,但可指定开始位置
LPAD (string2 ,length ,pad ) //重复用pad加在string开头,直到字串长度为length
LTRIM (string2 ) //去除前端空格
REPEAT (string2 ,count ) //重复count次
REPLACE (str ,search_str ,replace_str ) //在str中用replace_str替换search_str
RPAD (string2 ,length ,pad) //在str后用pad补充,直到长度为length
RTRIM (string2 ) //去除后端空格
STRCMP (string1 ,string2 ) //逐字符比较两字串大小,
SUBSTRING (str , position [,length ]) //从str的position开始,取length个字符,
② 数学类
ABS (number2 ) //绝对值
BIN (decimal_number ) //十进制转二进制
CEILING (number2 ) //向上取整
CONV(number2,from_base,to_base) //进制转换
FLOOR (number2 ) //向下取整
FORMAT (number,decimal_places ) //保留小数位数
HEX (DecimalNumber ) //转十六进制
注:HEX()中可传入字符串,则返回其ASC-11码,如HEX('DEF')返回4142143
也可以传入十进制整数,返回其十六进制编码,如HEX(25)返回19
LEAST (number , number2 [,..]) //求最小值
MOD (numerator ,denominator ) //求余
POWER (number ,power ) //求指数
RAND([seed]) //随机数
ROUND (number [,decimals ]) //四舍五入,decimals为小数位数]
③ 日期时间类
ADDTIME (date2 ,time_interval ) //将time_interval加到date2
CONVERT_TZ (datetime2 ,fromTZ ,toTZ ) //转换时区
CURRENT_DATE ( ) //当前日期
CURRENT_TIME ( ) //当前时间
CURRENT_TIMESTAMP ( ) //当前时间戳
DATE (datetime ) //返回datetime的日期部分
DATE_ADD (date2 , INTERVAL d_value d_type ) //在date2中加上日期或时间
DATE_FORMAT (datetime ,FormatCodes ) //使用formatcodes格式显示datetime
DATE_SUB (date2 , INTERVAL d_value d_type ) //在date2上减去一个时间
DATEDIFF (date1 ,date2 ) //两个日期差
DAY (date ) //返回日期的天
DAYNAME (date ) //英文星期
DAYOFWEEK (date ) //星期(1-7) ,1为星期天
DAYOFYEAR (date ) //一年中的第几天
EXTRACT (interval_name FROM date ) //从date中提取日期的指定部分
MAKEDATE (year ,day ) //给出年及年中的第几天,生成日期串
MAKETIME (hour ,minute ,second ) //生成时间串
MONTHNAME (date ) //英文月份名
NOW ( ) //当前时间
SEC_TO_TIME (seconds ) //秒数转成时间
STR_TO_DATE (string ,format ) //字串转成时间,以format格式显示
TIMEDIFF (datetime1 ,datetime2 ) //两个时间差
TIME_TO_SEC (time ) //时间转秒数]
WEEK (date_time [,start_of_week ]) //第几周
YEAR (datetime ) //年份
DAYOFMONTH(datetime) //月的第几天
HOUR(datetime) //小时
LAST_DAY(date) //date的月的最后日期
MICROSECOND(datetime) //微秒
MONTH(datetime) //月
MINUTE(datetime) //分