MYSQL 存储过程通用

Posted 米蓝

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MYSQL 存储过程通用相关的知识,希望对你有一定的参考价值。

返回随机时间 函数

CREATE  FUNCTION `milan_get_rand_time`(type VARCHAR(50)) RETURNS varchar(20) CHARSET utf8
BEGIN
DECLARE _day INT DEFAULT (FLOOR(RAND() * 60)); -- 随机天数 60天以内随机天数
DECLARE _hour INT DEFAULT (FLOOR(RAND() * 24)); -- 随机小时
DECLARE _minute INT DEFAULT (FLOOR(RAND() * 60));-- 随机分
DECLARE _second INT DEFAULT (FLOOR(RAND() * 60));-- 随机秒
DECLARE _myday VARCHAR(20);
IF type = before THEN
        set _day =_day;
ELSEIF type=after THEN
        SET _day =-1*_day;
ELSE
        SET _day=0;
END IF;

 SET _myday = DATE_FORMAT(DATE_SUB(NOW(),INTERVAL _day DAY) ,%Y-%m-%d); -- 获取一个随机的日期
-- SET _myday = DATE_SUB(CURRENT_DATE(),INTERVAL _day DAY); -- 获取一个随机的日期
SET _myday = CONCAT(_myday, ,LPAD(_hour,2,0),:,LPAD(_minute,2,0),:,LPAD(_second,2,0)); -- 在这个日期上加上时分秒
   RETURN _myday;
END

带参数的函数

CREATE  FUNCTION `delete_t_mer_info_group`(`_uid` int) RETURNS int(11)
BEGIN
    #Routine body goes here...
DELETE from t_mer where uid=_uid;
DELETE from t_mer_info where uid=_uid;
DELETE from t_mer_group where member_uid =_uid or leader_uid=_uid;
    RETURN 0;


END

游标test

CREATE  PROCEDURE `curtest`()
BEGIN
DECLARE _uid INT;
DECLARE _miaohao VARCHAR(20);
DECLARE done INT DEFAULT FALSE;
 -- 遍历数据结束标志
DECLARE cur CURSOR FOR SELECT uid,miaohao FROM tmm_tmp_testaccount ;
  -- 将结束标志绑定到游标
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
  OPEN cur;
  read_loop: LOOP
    FETCH cur INTO _uid,_miaohao;
   IF done THEN
      LEAVE read_loop;
    END IF;
    UPDATE tmm_tmp_testaccount set `desc` = NULL where uid =_uid;
  END LOOP;
  CLOSE cur;
END

while循环

CREATE  PROCEDURE `add_t_statis_member_1`(in _member_uid int,in _leader_uid int,in _dayCount int)
BEGIN

DECLARE _count INT;
DECLARE _statis_date VARCHAR(20);

DELETE FROM t_statis_member_1 where member_uid = _member_uid;

SET _count = _dayCount;

WHILE _count > 0 DO
set _statis_date = date_format(DATE_SUB(NOW(),INTERVAL +_count DAY), %Y-%m-%d);



insert into t_statis_member_1(statis_date,member_uid,new_platform_user,new_game_user,active_user,pay_user,total_pay,total_order_price,leader_uid)
 values 
(
_statis_date,/*日期_varchar(16)_NO_statis_date*/
_member_uid,/*组员uid_bigint(20)_YES_member_uid*/
1,/*新增注册_int(11) unsigned zerofill_NO_new_platform_user*/
2,/*新增游戏注册_int(11)_NO_new_game_user*/
3,/*活跃人数_int(11)_NO_active_user*/
4,/*付费人数_int(11)_NO_pay_user*/
5.01,/*总付费金额_decimal(10,2)_NO_total_pay*/
6.01,/*总订单金额_decimal(10,2)_NO_total_order_price*/
_leader_uid /*所属组长uid_bigint(20)_YES_leader_uid*/);


set _count =_count-1;

END WHILE;

select * from t_statis_member_1 where member_uid = _member_uid;

END

拼接时间时分秒

CONCAT(_myday, ,LPAD(_hour,2,0),:,LPAD(_minute,2,0),:,LPAD(_second,2,0)); -- 在这个日期上加上时分秒

生成随机ID

CREATE FUNCTION `get_id`() RETURNS bigint(20)
BEGIN
    #Routine body goes here...

    RETURN CONCAT(UNIX_TIMESTAMP(NOW())-FLOOR(RAND() * 10000),FLOOR(RAND() * 10));
END

 

以上是关于MYSQL 存储过程通用的主要内容,如果未能解决你的问题,请参考以下文章

mybatis通用功能代码生成工具

MySql触发器使用

在通用 DAO 中使用 Hibernate 调用存储过程的最佳方法是啥?

MySQL触发器

Informix 存储过程通用异常处理

MySql 存储过程搜索喜欢