mysql存储过程

DELIMITER $$

DROP PROCEDURE IF EXISTS `lekan`.`UpdateTagStats` $$
CREATE DEFINER=`root`@`%` PROCEDURE `UpdateTagStats`( in vdate varchar(50) )
begin
    declare status int;
    declare vtagId bigint;
    declare vnum int;
     declare done int DEFAULT 0;
   
    declare cur1 cursor for     
    SELECT vt.tagId,count(vt.tagId) num
                FROM user_watched_video uv,video_tag vt
         where uv.videoId=vt.videoId and createDate>vdate group by vt.tagId order by num desc;
    DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done = 1;

    open cur1;
    repeat
        fetch cur1 into vtagId,vnum;
            select count(tagId) into status from tag_stats where tagId=vtagId;
            if not done then
                if status>0 then
                    update tag_stats set num=vnum where tagId=vtagId;
                else
                     insert into tag_stats(tagId,num ) values(vtagId,vnum);
                end if;
            end if;
    until done end repeat;
    close cur1;
end $$

DELIMITER ;

你可能感兴趣的:(mysql,存储过程)