mysql存储过程总结

mysql存储过程的例子:

DELIMITER $$

DROP PROCEDURE IF EXISTS `cn_games`.`proc_pay_fill_rule`$$

CREATE DEFINER=`root`@`%` PROCEDURE `proc_pay_fill_rule`(IN strdate varchar(10))
BEGIN
declare done int;
DECLARE price float;
DECLARE cpid varchar(10);
DECLARE producttype varchar(10);
DECLARE startvalue bigint(20);
DECLARE endvalue bigint(20);
DECLARE cppercent float;
/*通用规则*/
DECLARE pay_rule_cursor cursor for SELECT cp_id,product_type, start_value,end_value,cp_percent from t_cp_percent
WHERE EXTRACT(YEAR_MONTH from start_date)<=EXTRACT(YEAR_MONTH from strdate)
and EXTRACT(YEAR_MONTH from end_date)>=EXTRACT(YEAR_MONTH from strdate)
and cp_id='0' and product_type='';
/*特殊cp的支付规则*/
DECLARE special_pay_rule_cursor cursor for SELECT cp_id,product_type, start_value,end_value,cp_percent from t_cp_percent
WHERE EXTRACT(YEAR_MONTH from start_date)<=EXTRACT(YEAR_MONTH from strdate)
and EXTRACT(YEAR_MONTH from end_date)>=EXTRACT(YEAR_MONTH from strdate)
and cp_id!='0';

/*特殊cp的productType(产品类型)支付规则*/
DECLARE special_pt_pay_rule_cursor cursor for SELECT cp_id,product_type, start_value,end_value,cp_percent from t_cp_percent
WHERE EXTRACT(YEAR_MONTH from start_date)<=EXTRACT(YEAR_MONTH from strdate)
and EXTRACT(YEAR_MONTH from end_date)>=EXTRACT(YEAR_MONTH from strdate)
and cp_id!='0' and product_type!='';

declare continue handler for not found set done=1;

drop table if exists TMP_t_pay_month;

CREATE TEMPORARY TABLE `TMP_t_pay_month` (
`stat_date` varchar(15) default NULL,
`cp_id` varchar(10) default NULL,
`product_type` varchar(10) default NULL,
`stat_num` varchar(20) default NULL,
`stat_totle_fee` varchar(20) default NULL,
`stat_finally_fee` varchar(20) default NULL,
`cp_percent` float default NULL
) type heap;


if strdate is not null THEN
/*对第二次出帐进行限制*/
if exists (select id from t_pay_month where left(stat_date,7)=left(strdate,7) limit 0,1) then
select "1";
else
/*把数据导入到临时表中*/
insert into TMP_t_pay_month (stat_date,cp_id,product_type,stat_num,stat_totle_fee,stat_finally_fee,cp_percent)
select left(stat_date,7),cp_id,product_type,sum(orders),sum(income),0,0
from t_cp_report
group by left(stat_date,7),cp_id,product_type;
/*通用的支付规则开始循环*/
set done=0;
open pay_rule_cursor;
loop1:loop
/*把值插入到变量中*/
fetch pay_rule_cursor into cpid,producttype,startvalue,endvalue,cppercent;
if done=1 then
leave loop1;
end if;
/*更新临时表中的比值*/
update TMP_t_pay_month set cp_percent=cppercent
where stat_totle_fee>=startvalue and stat_totle_fee<endvalue;
end loop loop1;
close pay_rule_cursor;

/*特殊cp的支付规则开始循环*/
set done=0;
open special_pay_rule_cursor;
loop2:loop
fetch special_pay_rule_cursor into cpid,producttype,startvalue,endvalue,cppercent;
if done=1 then
leave loop2;
end if;
update TMP_t_pay_month set cp_percent=cppercent
where stat_totle_fee>=startvalue and stat_totle_fee<endvalue
and cp_id=cpid;
end loop loop2;
CLOSE special_pay_rule_cursor;

/*特殊cp的产品的支付规则开始循环*/
set done=0;
open special_pt_pay_rule_cursor;
loop3:loop
fetch special_pt_pay_rule_cursor into cpid,producttype,startvalue,endvalue,cppercent;
if done=1 then
leave loop3;
end if;
update TMP_t_pay_month set cp_percent=cppercent
where stat_totle_fee>=startvalue and stat_totle_fee<endvalue
and cp_id=cpid and product_type=producttype;
end loop loop3;
CLOSE special_pt_pay_rule_cursor;

/*更新临时表中的最终支付的字段的值*/
update TMP_t_pay_month set stat_finally_fee=ROUND(stat_totle_fee*cp_percent);

/*把数据由临时表插入到正式表中,目前因为测试把cp_percent字段的值为总金额*/
insert into t_pay_month (stat_date,cp_id,product_type,stat_num, stat_fee,cp_percent, pass_status, pay_status, create_date)
select stat_date,cp_id,product_type,stat_num,stat_finally_fee,cp_percent,0,0,now()
from TMP_t_pay_month;
end if;
end if;
END$$

DELIMITER ;


一.创建存储过程
1.基本语法:
CREATE DEFINER=`root`@`%` PROCEDURE `proc_pay_fill_rule`(IN strdate varchar(10))
begin
.........
end
2.参数传递
`proc_pay_fill_rule`(IN strdate varchar(10))
strdate参数从调用的时候传入,例如:call proc_pay_fill_rule('2009-02-01')
二.调用存储过程
1.基本语法:call sp_name()
注意:存储过程名称后面必须加括号,哪怕该存储过程没有参数传递
三.删除存储过程
1.基本语法:
drop procedure sp_name//
2.注意事项
(1)不能在一个存储过程中删除另一个存储过程,只能调用另一个存储过程
四.区块,条件,循环
1.区块定义,常用
begin
......
end;
也可以给区块起别名,如:
lable:begin
...........
end lable;
可以用leave lable;跳出区块,执行区块以后的代码
2.条件语句
if 条件 then
statement
else
statement
end if;
如:

if exists (select id from t_pay_month where left(stat_date,7)=left(strdate,7) limit 0,1) then
statement
else
statement


是判断某一个数据库是否有数据。

3.循环语句
1)首先定义游标,语法如:DECLARE ... CURSOR FOR SELECT ... FROM ... 例子如下:

DECLARE pay_rule_cursor cursor for SELECT cp_id,product_type, start_value,end_value,cp_percent from t_cp_percent
WHERE EXTRACT(YEAR_MONTH from start_date)<=EXTRACT(YEAR_MONTH from strdate)
and EXTRACT(YEAR_MONTH from end_date)>=EXTRACT(YEAR_MONTH from strdate)
and cp_id='0' and product_type='';

2)声明一个游标使用的变量,(用处不是很明白,在打开游标之前还要set done=0;好像是用于跳出循环用的,)如:declare continue handler for not found set done=1;
2)打开游标,open ...;如:open pay_rule_cursor.
3)开始循环,name:loop 如:loop1:loop
4)把游标的值插入到声明的变量中去,语法:fetch ...(游标名) into ....(变量名列表),如:
fetch pay_rule_cursor into cpid,producttype,startvalue,endvalue,cppercent;

5)接着加上

if done=1 then
leave loop1;
end if;

好像是跳出循环用。
6)接着就是执行需要的操作,如:

update TMP_t_pay_month set cp_percent=cppercent
where stat_totle_fee>=startvalue and stat_totle_fee<endvalue;

7)结束循环,如:end loop loop1;
8)关闭游标,如:CLOSE pay_rule_cursor;

我是采用了这个流程才正确执行了循环。

五.其他常用命令
1.show procedure status
显示数据库中所有存储的存储过程基本信息,包括所属数据库,存储过程名称,创建时间等
2.show create procedure sp_name
显示某一个存储过程的详细信息

参考文章:http://blog.csdn.net/crazy_rain/archive/2007/07/06/1680799.aspx
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值