存储过程总结

mysql存储过程的例子: 

Sql代码   收藏代码
  1. DELIMITER $$  
  2.   
  3. DROP PROCEDURE IF EXISTS `cn_games`.`proc_pay_fill_rule`$$  
  4.   
  5. CREATE DEFINER=`root`@`%` PROCEDURE `proc_pay_fill_rule`(IN strdate varchar(10))  
  6. BEGIN  
  7. declare done int;  
  8. DECLARE price float;  
  9. DECLARE cpid varchar(10);  
  10. DECLARE producttype varchar(10);  
  11. DECLARE startvalue bigint(20);  
  12. DECLARE endvalue bigint(20);  
  13. DECLARE cppercent float;  
  14. /*通用规则*/  
  15. DECLARE pay_rule_cursor cursor for SELECT cp_id,product_type, start_value,end_value,cp_percent from t_cp_percent   
  16. WHERE EXTRACT(YEAR_MONTH from start_date)<=EXTRACT(YEAR_MONTH from strdate)  
  17. and EXTRACT(YEAR_MONTH from end_date)>=EXTRACT(YEAR_MONTH from strdate)  
  18. and cp_id='0' and product_type='';  
  19. /*特殊cp的支付规则*/  
  20. DECLARE special_pay_rule_cursor cursor for SELECT cp_id,product_type, start_value,end_value,cp_percent from t_cp_percent   
  21. WHERE EXTRACT(YEAR_MONTH from start_date)<=EXTRACT(YEAR_MONTH from strdate)  
  22. and EXTRACT(YEAR_MONTH from end_date)>=EXTRACT(YEAR_MONTH from strdate)  
  23. and cp_id!='0';  
  24.   
  25. /*特殊cp的productType(产品类型)支付规则*/  
  26. DECLARE special_pt_pay_rule_cursor cursor for SELECT cp_id,product_type, start_value,end_value,cp_percent from t_cp_percent   
  27. WHERE EXTRACT(YEAR_MONTH from start_date)<=EXTRACT(YEAR_MONTH from strdate)  
  28. and EXTRACT(YEAR_MONTH from end_date)>=EXTRACT(YEAR_MONTH from strdate)  
  29. and cp_id!='0' and product_type!='';  
  30.   
  31. declare continue handler for not found set done=1;  
  32.   
  33. drop table if exists TMP_t_pay_month;  
  34.   
  35. CREATE TEMPORARY TABLE `TMP_t_pay_month` (                
  36.                `stat_date` varchar(15) default NULL,                           
  37.                `cp_id` varchar(10) default NULL,                                  
  38.                `product_type` varchar(10) default NULL,                           
  39.                `stat_num` varchar(20) default NULL,   
  40.            `stat_totle_fee` varchar(20) default NULL,  
  41.                `stat_finally_fee` varchar(20) default NULL,                               
  42.                `cp_percent` float default NULL                                            
  43.              ) type heap;  
  44.   
  45.   
  46.   
  47. if strdate is not null THEN  
  48.     /*对第二次出帐进行限制*/  
  49.     if exists (select id from t_pay_month where left(stat_date,7)=left(strdate,7) limit 0,1) then  
  50.         select "1";  
  51.     else  
  52.         /*把数据导入到临时表中*/  
  53.         insert into TMP_t_pay_month (stat_date,cp_id,product_type,stat_num,stat_totle_fee,stat_finally_fee,cp_percent)  
  54.         select  left(stat_date,7),cp_id,product_type,sum(orders),sum(income),0,0  
  55.         from t_cp_report  
  56.         group by  left(stat_date,7),cp_id,product_type;  
  57.         /*通用的支付规则开始循环*/  
  58.         set done=0;  
  59.         open pay_rule_cursor;  
  60.         loop1:loop    
  61.         /*把值插入到变量中*/  
  62.         fetch pay_rule_cursor into cpid,producttype,startvalue,endvalue,cppercent;  
  63.         if done=1 then  
  64.             leave loop1;  
  65.         end if;  
  66.             /*更新临时表中的比值*/  
  67.             update TMP_t_pay_month set cp_percent=cppercent  
  68.             where stat_totle_fee>=startvalue and stat_totle_fee<endvalue;           
  69.         end loop loop1;  
  70.         close pay_rule_cursor;  
  71.   
  72.         /*特殊cp的支付规则开始循环*/  
  73.         set done=0;  
  74.         open special_pay_rule_cursor;  
  75.         loop2:loop  
  76.         fetch special_pay_rule_cursor into cpid,producttype,startvalue,endvalue,cppercent;  
  77.         if done=1 then  
  78.             leave loop2;  
  79.         end if;  
  80.             update TMP_t_pay_month set cp_percent=cppercent  
  81.             where stat_totle_fee>=startvalue and stat_totle_fee<endvalue  
  82.             and cp_id=cpid;  
  83.         end loop loop2;  
  84.         CLOSE special_pay_rule_cursor;  
  85.           
  86.         /*特殊cp的产品的支付规则开始循环*/  
  87.         set done=0;  
  88.         open special_pt_pay_rule_cursor;  
  89.         loop3:loop  
  90.         fetch special_pt_pay_rule_cursor into cpid,producttype,startvalue,endvalue,cppercent;  
  91.         if done=1 then  
  92.             leave loop3;  
  93.         end if;  
  94.             update TMP_t_pay_month set cp_percent=cppercent  
  95.             where stat_totle_fee>=startvalue and stat_totle_fee<endvalue  
  96.             and cp_id=cpid and product_type=producttype;  
  97.         end loop loop3;  
  98.         CLOSE special_pt_pay_rule_cursor;  
  99.           
  100.         /*更新临时表中的最终支付的字段的值*/  
  101.         update TMP_t_pay_month set stat_finally_fee=ROUND(stat_totle_fee*cp_percent);  
  102.   
  103.         /*把数据由临时表插入到正式表中,目前因为测试把cp_percent字段的值为总金额*/  
  104.         insert into t_pay_month (stat_date,cp_id,product_type,stat_num, stat_fee,cp_percent, pass_status, pay_status, create_date)  
  105.         select stat_date,cp_id,product_type,stat_num,stat_finally_fee,cp_percent,0,0,now()  
  106.         from TMP_t_pay_month;  
  107.     end if;  
  108. end if;  
  109.     END$$  
  110.   
  111. 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;
如: 

Sql代码   收藏代码
  1. if exists (select id from t_pay_month where left(stat_date,7)=left(strdate,7) limit 0,1) then  
  2.     statement  
  3. else  
  4.     statement  



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

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

Sql代码   收藏代码
  1. DECLARE pay_rule_cursor cursor for SELECT cp_id,product_type, start_value,end_value,cp_percent from t_cp_percent   
  2. WHERE EXTRACT(YEAR_MONTH from start_date)<=EXTRACT(YEAR_MONTH from strdate)  
  3. and EXTRACT(YEAR_MONTH from end_date)>=EXTRACT(YEAR_MONTH from strdate)  
  4. 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 ....(变量名列表),如: 

Sql代码   收藏代码
  1. fetch pay_rule_cursor into cpid,producttype,startvalue,endvalue,cppercent;  


5)接着加上

Sql代码   收藏代码
  1. if done=1 then  
  2.     leave loop1;  
  3. end if;  


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

Sql代码   收藏代码
  1. update TMP_t_pay_month set cp_percent=cppercent  
  2. 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 

转自:http://canofy.iteye.com/blog/343443

猜你喜欢

转载自lvwenwen.iteye.com/blog/2175175