MySql存储过程

mysql 存储过程编写: 

CURRENT_USER PROCEDURE `sq_congratulationProc`
BEGIN
-- 需要定义接收游标数据的变量 
DECLARE uid int(8);
DECLARE cgln varchar(60) character set utf8;
DECLARE temp varchar(60) character set utf8;	
DECLARE usern varchar(60)  character set utf8;
DECLARE realn varchar(255)  character set utf8;
DECLARE userm decimal(10) ;
DECLARE ct int(10);
-- 遍历数据结束标志
  DECLARE done INT DEFAULT FALSE;

  -- 游标
  DECLARE cur CURSOR FOR SELECT t2.user_id,t2.user_name,t2.real_name,t2.user_money,t2.change_time FROM (SELECT t1.*,u1.user_name,u1.real_name from (select g.user_id,g.user_money,u.user_name as btj_name, change_time from (ecs_account_log g LEFT JOIN ecs_users u  on g.from_key=u.user_id ) where u.user_rank=4 and g.from_type=1 and g.user_id=u.parent_id) t1 left JOIN ecs_users u1 on  t1.user_id=u1.user_id) t2;
  
  -- 将结束标志绑定到游标
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
  -- 打开游标
  OPEN cur;
  
  -- 开始循环
  read_loop: LOOP
    -- 提取游标里的数据,这里只有一个,多个的话也一样;
    FETCH cur INTO uid,usern,realn,userm,ct;
    -- 声明结束的时候
    IF done THEN
      LEAVE read_loop;
    END IF;
    -- 这里做你想做的循环的事件
    set cgln=realn;
		IF cgln IS  NULL or cgln ='' THEN
			SET cgln=usern;
		END IF;

		IF cgln is not NULL and cgln<>'' THEN
			SELECT cgln REGEXP '^[1][35678][0-9]{9}$' INTO temp;
			IF temp=1 THEN
				SET cgln=REPLACE(cgln, SUBSTR(cgln,4,4), '****');
			end if;
			INSERT INTO ecs_congratulate_info (`user_id`, `user_name`, `congratulate_info`, `type_id`, `create_time`)
																			VALUES(uid,usern,FORMAT(userm,0),4,ct);
		END IF;

  END LOOP;
  -- 关闭游标
  CLOSE cur;

END


你可能感兴趣的:(MySql)