『转』MySQL存储过程语法例子

原文地址:http://blog.chinaunix.net/uid-540802-id-138873.html

------------------------- 自动生成随机数据存储过程 -------------------------
drop PROCEDURE if exists genRand;
delimiter //
create PROCEDURE genRand(in rank int, in add_num int, in statTime char(10))
-- rank:随机最大邮件数, add_num:生成条数, startTime:统计数据日期
Begin
    declare mobile long;
    declare tmp int;
    set mobile = 13600000000;
    set tmp = 1;
    -- delete from ippush_stats_email;
    while tmp <= add_num do
        insert into ippush_stats_email values(mobile + tmp, 1, now(), statTime,
                                                  random_int(rank), random_int(rank), random_int(rank), random_int(rank),random_int(rank),
                                                  random_int(rank), random_int(rank), random_int(rank), random_int(rank),random_int(rank), 
                                                  random_int(rank), random_int(rank), random_int(rank), random_int(rank),random_int(rank), 
                                                  random_int(rank), random_int(rank), random_int(rank), random_int(rank),random_int(rank), 
                                                  random_int(rank), random_int(rank), random_int(rank), random_int(rank) );
        set tmp = tmp + 1;
    end while;
end//
delimiter ;
call genRand(10, 10, ‘2010-10-6‘);

------------------------- 统计从time1到time2时段的 -------------------------
drop PROCEDURE if exists doStats;
delimiter //
create PROCEDURE doStats(in time1 char(10), in time2 char(10))
Begin
    declare _mobile char(11);
    declare _sums int;
    declare _stat_time date;
    declare _count int;
    declare _user_type int;
    declare fetchSeqOk int;
    declare fetchSeqCursor cursor for 
                                select mobile, count(*) as count, user_type, stat_time, max((clock_1 + clock_2 + clock_3+clock_4+clock_5+clock_6+clock_7+clock_8+clock_9+
                                    clock_10+clock_11+clock_12+clock_13+clock_14+clock_15+clock_16+clock_17+clock_18+clock_19+
                                    clock_20+clock_21+clock_22+clock_23+clock_24)) as sums 
                                from ippush_stats_email
                                where stat_time >= time1 and stat_time <= time2
                                group by mobile;
    declare CONTINUE HANDLER FOR NOT FOUND SET fetchSeqOk = 0;

set fetchSeqOk = 1;
    open fetchSeqCursor;
    while fetchSeqOk = 1 do
            fetch fetchSeqCursor into _mobile, _count, _user_type, _stat_time, _sums;
            if _sums > 30 then
                call onelineStat(1, _user_type, time1, time2, _mobile);
            else
                call onelineStat(2, _user_type, time1, time2, _mobile);
            end if;
    end while;
    close fetchSeqCursor;
end//
delimiter ;
call doStats(‘2010-10-01‘, ‘2010-11-05‘);

------------------------- 一条业务线的统计 --------------------------
drop PROCEDURE if exists onelineStat;
delimiter //
create PROCEDURE onelineStat(in type int, in inuser_type int, in time1 char(10), in time2 char(10), in inmobile char(11))
Begin
    if inuser_type = 1 then -- is mas user
        if type = 1 then -- is level 1
            insert into ippush_strategy (select mobile, inuser_type, concat(max(clock_1)>=4,
                    max(clock_2)>=4,
                    max(clock_3)>=4,
                    max(clock_4)>=4,
                    max(clock_5)>=4,
                    max(clock_6)>=4,
                    max(clock_7)>=4,
                    max(clock_8)>=4,
                    ‘111111111‘,
                    max(clock_19)>=4,
                    max(clock_20)>=4,
                    max(clock_21)>=4,
                    max(clock_22)>=4,
                    max(clock_23)>=4,
                    max(clock_24)>=4), now(), time1, time2
                from ippush_stats_email
                where mobile = inmobile and stat_time >= time1 and stat_time <= time2 and user_type = inuser_type
                group by mobile ) ;
        else
            insert into ippush_strategy ( select mobile, inuser_type, concat(max(clock_1)>=4,
                    max(clock_2)>=4,
                    max(clock_3)>=4,
                    max(clock_4)>=4,
                    max(clock_5)>=4,
                    max(clock_6)>=4,
                    max(clock_7)>=4,
                    max(clock_8)>=4,
                    max(clock_9)>=4,
                    max(clock_10)>=4,
                    max(clock_11)>=4,
                    max(clock_12)>=4,
                    max(clock_13)>=4,
                    max(clock_14)>=4,
                    max(clock_15)>=4,
                    max(clock_16)>=4,
                    max(clock_17)>=4,
                    max(clock_18)>=4,
                    max(clock_19)>=4,
                    max(clock_20)>=4,
                    max(clock_21)>=4,
                    max(clock_22)>=4,
                    max(clock_23)>=4,
                    max(clock_24)>=4), now(), time1, time2
                from ippush_stats_email
                where mobile = inmobile and stat_time >= time1 and stat_time <= time2 and user_type = inuser_type
                group by mobile ) ;
        end if;
   else -- is mig user
        if type = 1 then
            insert into ippush_strategy values(inmobile, inuser_type, ‘111111111111111111111111‘, now, time1, time2);
        end if;
   end if;
end//
delimiter ;

------------------ 调用 --------------------
call onelineStat(1, 1, ‘2010-10-01‘, ‘2010-11-05‘, ‘13600000001‘);

时间: 2024-08-24 15:02:32

『转』MySQL存储过程语法例子的相关文章

mysql存储过程语法及实例

存储过程如同一门程序设计语言,同样包含了数据类型.流程控制.输入和输出和它自己的函数库. --------------------基本语法-------------------- 一.创建存储过程create procedure sp_name()begin.........end 二.调用存储过程1.基本语法:call sp_name()注意:存储过程名称后面必须加括号,哪怕该存储过程没有参数传递 三.删除存储过程1.基本语法:drop procedure sp_name// 2.注意事项(1

MySQL存储过程语法

create procedure t_insert_table()  begin      /** 标记是否出错 */      declare t_error int default 0;      /** 如果出现sql异常,则将t_error设置为1后继续执行后面的操作 */      declare continue handler for sqlexception set t_error=1; -- 出错处理      /** 显式的开启事务,它开启后,事务会暂时停止自动提交*/   

『浅入浅出』MySQL 和 InnoDB

作为一名开发人员,在日常的工作中会难以避免地接触到数据库,无论是基于文件的 sqlite 还是工程上使用非常广泛的 MySQL.PostgreSQL,但是一直以来也没有对数据库有一个非常清晰并且成体系的认知,所以最近两个月的时间看了几本数据库相关的书籍并且阅读了 MySQL 的官方文档,希望对各位了解数据库的.不了解数据库的有所帮助. 本文中对于数据库的介绍以及研究都是在 MySQL 上进行的,如果涉及到了其他数据库的内容或者实现会在文中单独指出. 数据库的定义 很多开发者在最开始时其实都对数据

重新学习MySQL数据库2:『浅入浅出』MySQL 和 InnoDB

重新学习Mysql数据库2:『浅入浅出』MySQL 和 InnoDB 作为一名开发人员,在日常的工作中会难以避免地接触到数据库,无论是基于文件的 sqlite 还是工程上使用非常广泛的 MySQL.PostgreSQL,但是一直以来也没有对数据库有一个非常清晰并且成体系的认知,所以最近两个月的时间看了几本数据库相关的书籍并且阅读了 MySQL 的官方文档,希望对各位了解数据库的.不了解数据库的有所帮助. 本文中对于数据库的介绍以及研究都是在 MySQL 上进行的,如果涉及到了其他数据库的内容或者

Mysql 存储过程基本语法

delimiter //一般情况下MYSQL以:结尾表示确认输入并执行语句,但在存储过程中:不是表示结束,因此可以用该命令将:号改为//表示确认输入并执行. 一.创建存储过程 1.基本语法: create procedure sp_name() begin ......... end 2.参数传递 二.调用存储过程 1.基本语法:call sp_name() 注意:存储过程名称后面必须加括号,哪怕该存储过程没有参数传递 三.删除存储过程 1.基本语法: drop procedure sp_nam

MySQL带参数的存储过程小例子

http://wwty.iteye.com/blog/698239 mysql存储过程也提供了对异常处理的功能:通过定义HANDLER来完成异常声明的实现 语法如下: DECLARE handler_type HANDLER FOR condition_value[,...] sp_statement handler_type: CONTINUE | EXIT condition_value: SQLSTATE [VALUE] sqlstate_value | condition_name |

Mysql Insert Or Update语法例子

Mysql Insert Or Update语法例子 有的时候会需要写一段insert的sql,如果主键存在,则update:如果主键不存在,则insert.Mysql中提供了这样的用法:ON DUPLICATE KEY UPDATE.下面就看看它是如何使用的吧! 首先数据库的原始数据如下: a b c 1 b1 c1 2 b2 c2 3 b3 c3 此时如果执行下面的sql就会报错 INSERT INTO test VALUES(1,'b4','c4'); 报错信息如下,提示无法重复插入: 1

MYSql存储过程的作用及语法

1.使用了存过程,很多相似性的删除,更新,新增等操作就变得轻松了,并且以后也便于管理! 2.存储过程因为SQL语句已经预编绎过了,因此运行的速度比较快. 3.存储过程可以接受参数.输出参数.返回单个或多个结果集以及返回值.可以向程序返回错误原因. 4.存储过程运行比较稳定,不会有太多的错误.只要一次成功,以后都会按这个程序运行. 5.存储过程主要是在服务器上运行,减少对客户机的压力. 6.存储过程可以包含程序流.逻辑以及对数据库的查询.同时可以实体封装和隐藏了数据逻辑. 7.存储过程可以在单个存

『浅入深出』MySQL 中事务的实现

在关系型数据库中,事务的重要性不言而喻,只要对数据库稍有了解的人都知道事务具有 ACID 四个基本属性,而我们不知道的可能就是数据库是如何实现这四个属性的:在这篇文章中,我们将对事务的实现进行分析,尝试理解数据库是如何实现事务的,当然我们也会在文章中简单对 MySQL 中对 ACID 的实现进行简单的介绍. 事务其实就是并发控制的基本单位:相信我们都知道,事务是一个序列操作,其中的操作要么都执行,要么都不执行,它是一个不可分割的工作单位:数据库事务的 ACID 四大特性是事务的基础,了解了 AC