mysql 5.0存储过程学习总结
mysql存储过程的创建,删除,调用及其他常用命令
mysql 5.0存储过程学习总结一.创建存储过程
1.基本语法:create procedure sp_name()begin
………
end
2.参数传递
二.调用存储过程
1.基本语法:call sp_name()注意:存储过程名称后面必须加括号,哪怕该存储过程没有参数传递
三.删除存储过程
1.基本语法:drop procedure sp_name//
2.注意事项
(1)不能在一个存储过程中删除另一个存储过程,只能调用另一个存储过程
四.区块,条件,循环
begin
……
end;
也可以给区块起别名,如:
lable:begin
………..
end lable;
可以用leave lable;跳出区块,执行区块以后的代码
2.条件语句 if 条件 then
statement
else
statement
end if;
3.循环语句
(1).while循环
statements
END WHILE [label] ;
(2).loop循环 [label:] LOOP
statements
END LOOP [label];(3).repeat until循环 [label:] REPEAT
statements
UNTIL expression
END REPEAT [label] ;
五.其他常用命令
1.show procedure status显示数据库中所有存储的存储过程基本信息,包括所属数据库,存储过程名称,创建时间等
2.show create procedure sp_name
显示某一个存储过程的详细信息
mysql存储过程中要用到的运算符
mysql存储过程学习总结-操作符算术运算符
+ 加 SET var1=2+2; 4
- 减 SET var2=3-2; 1
* 乘 SET var3=3*2; 6
/ 除 SET var4=10/3; 3.3333
DIV 整除 SET var5=10 DIV 3; 3
% 取模 SET var6=10%3 ; 1
比较运算符
> 大于 1>2 False< 小于 2<1 False
<= 小于等于 2<=2 True
>= 大于等于 3>=2 True
BETWEEN 在两值之间 5 BETWEEN 1 AND 10 True
NOT BETWEEN 不在两值之间 5 NOT BETWEEN 1 AND 10 False
IN 在集合中 5 IN (1,2,3,4) False
NOT IN 不在集合中 5 NOT IN (1,2,3,4) True
= 等于 2=3 False
<>, != 不等于 2<>3 False
<=> 严格比较两个NULL值是否相等 NULL<=>NULL True
LIKE 简单模式匹配 "Guy Harrison" LIKE "Guy%" True
REGEXP 正则式匹配 "Guy Harrison" REGEXP "[Gg]reg" False
IS NULL 为空 0 IS NULL False
IS NOT NULL 不为空 0 IS NOT NULL True
逻辑运算符
与(AND)TRUE
或(OR)
异或(XOR)
位运算符
| 位或
& 位与
<< 左移位
>> 右移位
~ 位非(单目运算,按位取反)
mysq存储过程中常用的函数,字符串类型操作,数学类,日期时间类。
mysql存储过程基本函数一.字符串类
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个字符,
注:mysql中处理字符串时,默认第一个字符下标为1,即参数position必须大于等于1mysql> select substring(’abcd’,0,2);
+———————–+
| substring(’abcd’,0,2) |
+———————–+
| |
+———————–+
1 row in set (0.00 sec)mysql> select substring(’abcd’,1,2);
+———————–+
| substring(’abcd’,1,2) |
+———————–+
| ab |
+———————–+
1 row in set (0.02 sec)
TRIM([[BOTH|LEADING|TRAILING] [padding] FROM]string2) //去除指定位置的指定字符
UCASE (string2 ) //转换成大写
RIGHT(string2,length) //取string2最后length个字符
SPACE(count) //生成count个空格
二.数学类
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为小数位数]注:返回类型并非均为整数,如:
(1)默认变为整形值
mysql> select round(1.23);
+————-+
| round(1.23) |
+————-+
| 1 |
+————-+
1 row in set (0.00 sec)
mysql> select round(1.56);
+————-+
| round(1.56) |
+————-+
| 2 |
+————-+
1 row in set (0.00 sec)
(2)可以设定小数位数,返回浮点型数据
mysql> select round(1.567,2);
+—————-+
| round(1.567,2) |
+—————-+
| 1.57 |
+—————-+
1 row in set (0.00 sec)
SIGN (number2 ) //返回符号,正负或0
SQRT(number2) //开平方
三.日期时间类
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) //分
附:可用在INTERVAL中的类型
DAY ,DAY_HOUR ,DAY_MINUTE ,DAY_SECOND ,HOUR ,HOUR_MINUTE ,HOUR_SECOND ,MINUTE ,MINUTE_SECOND,MONTH ,SECOND ,YEAR
DELIMITER $$ #更新订单行消费的balance和cash字段 DROP PROCEDURE IF EXISTS fix_cl_consumption_balence_related $$ CREATE PROCEDURE fix_cl_consumption_balence_related ( IN cl_id BIGINT (20), IN mydate DATETIME, cost DECIMAL (12, 2), cash DECIMAL (12, 2) ) BEGIN DECLARE no_more_sql INT DEFAULT 0 ; DECLARE temp_id BIGINT (20) ; DECLARE counter INT DEFAULT 0 ; DECLARE sql_cur CURSOR FOR SELECT cc.id FROM contract_line_consumption cc WHERE cc.`contract_line_id` = cl_id AND cc.`date` >= mydate ORDER BY cc.date ASC ; DECLARE CONTINUE HANDLER FOR NOT FOUND SET no_more_sql = 1 ; OPEN sql_cur ; REPEAT FETCH sql_cur INTO temp_id ; IF NOT no_more_sql THEN UPDATE contract_line_consumption cc SET cc.balance = cc.balance - cost, cc.balance_cash = cc.balance_cash - cash WHERE cc.id = temp_id ; SET counter = counter + 1 ; END IF ; UNTIL no_more_sql END REPEAT ; CLOSE sql_cur ; END $$ #更新recharge_record的cost,cost_cash,amount_availabe and cash_available DROP PROCEDURE IF EXISTS fix_recharge_record_for_single_contract $$ CREATE PROCEDURE fix_recharge_record_for_single_contract ( IN cl_id BIGINT (20), IN cost DECIMAL (12, 2), IN cash DECIMAL (12, 2) ) BEGIN DECLARE no_more_sql INT DEFAULT 0 ; DECLARE i_id BIGINT (20) ; DECLARE i_amount_available DECIMAL (12, 2) ; DECLARE i_cash_available DECIMAL (12, 2) ; DECLARE i_cost DECIMAL (12, 2) ; DECLARE i_cost_cash DECIMAL (12, 2) ; DECLARE i_ratio DECIMAL (12, 5) ; DECLARE temp_cost_cash DECIMAL (12, 2) ; DECLARE temp_amount DECIMAL (12, 2) ; DECLARE sql_cur CURSOR FOR SELECT rr.id, rr.amount_available, rr.cash_available, rr.cost, rr.cost_cash, rr.ratio FROM recharge_record rr WHERE rr.contract_line_id = cl_id ORDER BY rr.recharge_time ASC ; DECLARE CONTINUE HANDLER FOR NOT FOUND SET no_more_sql = 1 ; SET temp_amount = cost ; OPEN sql_cur ; REPEAT FETCH sql_cur INTO i_id, i_amount_available, i_cash_available, i_cost, i_cost_cash, i_ratio ; IF NOT no_more_sql THEN IF i_amount_available > 0 && i_cash_available > 0 THEN IF temp_amount <> 0 THEN IF temp_amount >= i_amount_available THEN SET temp_amount = temp_amount - i_amount_available ; UPDATE recharge_record rr SET rr.cost = rr.amount, rr.cost_cash = rr.cash WHERE rr.id = i_id ; UPDATE recharge_record rr SET rr.amount_available = 0, rr.cash_available = 0 WHERE rr.id = i_id ; ELSE SET temp_cost_cash = temp_amount * i_ratio ; UPDATE recharge_record rr SET rr.cost = rr.cost + temp_amount, rr.cost_cash = rr.cost_cash + temp_cost_cash, rr.amount_available = rr.amount_available - temp_amount, rr.cash_available = rr.cash_available - temp_cost_cash WHERE rr.id = i_id ; SET temp_amount = 0 ; END IF ; END IF ; END IF ; END IF ; UNTIL no_more_sql END REPEAT ; CLOSE sql_cur ; END $$ DROP PROCEDURE IF EXISTS fix_consumption_from_botton_to_top $$ CREATE PROCEDURE fix_consumption_from_botton_to_top ( IN i_adc_id BIGINT (20), IN i_cost DECIMAL (12, 2), IN i_cash DECIMAL (12, 2), IN i_date DATETIME, IN i_cl_id BIGINT (20) ) BEGIN #修复ad_consumption的cost,cash UPDATE ad_consumption SET cost = i_cost, cash = i_cash WHERE id = i_adc_id ; #更新contract_line_consumption的cost和cash UPDATE contract_line_consumption ac INNER JOIN (SELECT SUM(wc.`cost`) AS cost, SUM(wc.`cash`) AS cash, wc.`contract_line_id` AS contract_line_id FROM ad_consumption wc WHERE wc.`contract_line_id` = i_cl_id AND wc.`date` = i_date GROUP BY wc.`contract_line_id`) xx ON xx.`contract_line_id` = ac.`contract_line_id` SET ac.cost = xx.cost, ac.cash = xx.cash WHERE ac.`date` = i_date ; #更新订单行的balance和balence_cash(cc之前为0) UPDATE contract_line cl INNER JOIN contract_line_consumption cc ON cc.`contract_line_id` = cl.`contract_line_id` AND cc.date = i_date SET cl.`balance` = cl.`balance` - cc.`cost`, cl.`balance_cash` = cl.`balance_cash` - cc.`cash` WHERE cl.`contract_line_id` = i_cl_id ; #更新订单行消费的balance和cash字段 CALL fix_cl_consumption_balence_related (i_cl_id, i_date, i_cost, i_cash) ; #更新recharge_record的cost,cost_cash,amount_availabe and cash_available CALL fix_recharge_record_for_single_contract (i_cl_id, i_cost, i_cash) ; END $$ DELIMITER ; CALL fix_consumption_from_botton_to_top ( 341479, 2654, 2654 / 2, '2013-01-15', 58197 ) ; DROP PROCEDURE IF EXISTS fix_cl_consumption_balence_related ; DROP PROCEDURE IF EXISTS fix_recharge_record_for_single_contract ; DROP PROCEDURE IF EXISTS fix_consumption_from_botton_to_top ;