• MySQL存储过程和函数(六)


    1、存储过程练习
    (1)
    DROP PROCEDURE IF EXISTS test12;
    CREATE PROCEDURE test12()
    BEGIN
        DECLARE username VARCHAR(20);
        DECLARE age INT;
        DECLARE IDs INT;
        DECLARE LOGINIDs VARCHAR(40);
        DECLARE Done INT;
        DECLARE var INT;
        #申明游标
        DECLARE cur_user CURSOR FOR SELECT ID,LOGINID FROM `user`;
        #CURSOR end mark
        DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET Done = 1;
     
        SET age=7;
        IF age<6 THEN
                SELECT "age小于6";
        END IF;
        SET username = "db";
        SELECT username;
        #循环使用
        SET var=0;
        WHILE var<10 DO
            SET var = var+1;
        END WHILE;
        SELECT var;
     
        #游标使用
        OPEN cur_user;
        FETCH cur_user INTO IDs,LOGINIDs;
        SELECT LOGINIDs;
        WHILE Done<1 DO
            FETCH cur_user INTO IDs,LOGINIDs;
            SELECT IDs,LOGINIDs;
        END WHILE;   
     
        CLOSE cur_user;
    END
    (2)
    DROP PROCEDURE IF EXISTS test13;
    CREATE PROCEDURE test13(OUT temp_count INT)
    BEGIN   
        DECLARE IDs INT;
        DECLARE LOGINIDs VARCHAR(40);
        DECLARE Done INT DEFAULT 0;   
        #申明游标
        DECLARE cur_user CURSOR FOR SELECT ID,LOGINID FROM `user` LIMIT 20;
        #CURSOR end mark
        DECLARE CONTINUE HANDLER FOR NOT FOUND SET Done = 1;
     
      #DROP TEMPORARY TABLE IF EXISTS temp_table;
        CREATE TEMPORARY TABLE IF NOT EXISTS temp_table(
            ID INT PRIMARY KEY,
            LOGINID VARCHAR(40)
        )ENGINE=InnoDB;
        #游标使用
        OPEN cur_user;
        REPEAT   
            FETCH cur_user INTO IDs,LOGINIDs;
            IF Done=0 THEN
                INSERT INTO temp_table VALUES(IDs,LOGINIDs);
            END IF;
        UNTIL Done=1 END REPEAT;   
        SELECT * FROM temp_table;
        SELECT COUNT(1) INTO temp_count FROM temp_table;   
      CLOSE cur_user;
     
        TRUNCATE TABLE temp_table;
    END
    (3)
    DROP FUNCTION IF EXISTS test_fun;
    CREATE FUNCTION test_fun(a INT) RETURNS VARCHAR(300)
    BEGIN
        RETURN "hello";
    END
     
    SELECT test_fun(1)
    drop procedure if exists pro_rep_shadow_rs;   
    delimiter |   
    ----------------------------------   
    -- rep_shadow_rs   
    -- 用来处理信息的增加,更新和删除   
    -- 每次只更新上次以来没有做过的数据   
    -- 根据不同的标志位   
    -- 需要一个输出的参数,   
    -- 如果返回为0,则调用失败,事务回滚   
    -- 如果返回为1,调用成功,事务提交   
    --   
    -- 测试方法   
    -- call pro_rep_shadow_rs(@rtn);   
    -- select @rtn;   
    ----------------------------------   
    create procedure pro_rep_shadow_rs(out rtn int)   
    begin   
        -- 声明变量,所有的声明必须在非声明的语句前面   
        declare iLast_rep_sync_id int default -1;   
        declare iMax_rep_sync_id int default -1;   
        -- 如果出现异常,或自动处理并rollback,但不再通知调用方了   
        -- 如果希望应用获得异常,需要将下面这一句,以及启动事务和提交事务的语句全部去掉   
        declare exit handler for sqlexception rollback;   
        -- 查找上一次的   
        select eid into iLast_rep_sync_id from rep_de_proc_log where tbl='rep_shadow_rs';   
        -- 如果不存在,则增加一行   
        if iLast_rep_sync_id=-1 then   
          insert into rep_de_proc_log(rid,eid,tbl) values(0,0,'rep_shadow_rs');   
          set iLast_rep_sync_id = 0;   
        end if;   
           
        -- 下一个数字   
        set iLast_rep_sync_id=iLast_rep_sync_id+1;   
        -- 设置默认的返回值为0:失败   
        set rtn=0;   
           
        -- 启动事务   
        start transaction;   
        -- 查找最大编号   
        select max(rep_sync_id) into iMax_rep_sync_id from rep_shadow_rs;   
        -- 有新数据   
        if iMax_rep_sync_id>=iLast_rep_sync_id then   
            -- 调用   
            call pro_rep_shadow_rs_do(iLast_rep_sync_id,iMax_rep_sync_id);   
            -- 更新日志   
            update rep_de_proc_log set rid=iLast_rep_sync_id,eid=iMax_rep_sync_id where tbl='rep_shadow_rs';   
        end if;   
           
        -- 运行没有异常,提交事务   
        commit;   
        -- 设置返回值为1  
        set rtn=1;   
    end;   
    |   
    delimiter ;   
    drop procedure if exists pro_rep_shadow_rs_do;   
    delimiter |   
    ---------------------------------   
    -- 处理指定编号范围内的数据   
    -- 需要输入2个参数   
    -- last_rep_sync_id 是编号的最小值   
    -- max_rep_sync_id 是编号的最大值   
    -- 无返回值   
    ---------------------------------   
    create procedure pro_rep_shadow_rs_do(last_rep_sync_id int, max_rep_sync_id int)   
    begin   
        declare iRep_operationtype varchar(1);   
        declare iRep_status varchar(1);   
        declare iRep_Sync_id int;   
        declare iId int;   
        -- 这个用于处理游标到达最后一行的情况   
        declare stop int default 0;   
        -- 声明游标   
        declare cur cursor for select id,Rep_operationtype,iRep_status,rep_sync_id from rep_shadow_rs where rep_sync_id between last_rep_sync_id and max_rep_sync_id;   
        -- 声明游标的异常处理,设置一个终止标记   
        declare CONTINUE HANDLER FOR SQLSTATE '02000' SET stop=1;   
           
        -- 打开游标   
        open cur;   
           
        -- 读取一行数据到变量   
        fetch cur into iId,iRep_operationtype,iRep_status,iRep_Sync_id;   
        -- 这个就是判断是否游标已经到达了最后   
        while stop <> 1 do  
            -- 各种判断   
            if iRep_operationtype='I' then   
                insert into rs0811 (id,fnbm) select id,fnbm from rep_shadow_rs where rep_sync_id=iRep_sync_id;   
            elseif iRep_operationtype='U' then   
            begin   
                if iRep_status='A' then   
                    insert into rs0811 (id,fnbm) select id,fnbm from rep_shadow_rs where rep_sync_id=iRep_sync_id;   
                elseif iRep_status='B' then   
                    delete from rs0811 where id=iId;   
                end if;   
            end;   
            elseif iRep_operationtype='D' then   
                delete from rs0811 where id=iId;   
            end if;    
               
            -- 读取下一行的数据    
            fetch cur into iId,iRep_operationtype,iRep_status,iRep_Sync_id;   
        end while;  -- 循环结束   
        close cur; -- 关闭游标   
     end;   
    |  
  • 相关阅读:
    fetch的使用--当无法判断后台返回数据为什么类型时如何操作
    单页面与多页面间的区别及优缺点
    关于倒计时在关屏后不准确的问题
    前端分页仿百度分页效果
    pc端的弹性布局适配方案
    前端性能优化方向
    居民身份证号码组成规则
    axios简单介绍
    es6 promise 简单总结
    js原型链和原型链的继承
  • 原文地址:https://www.cnblogs.com/b-dong/p/6305530.html
Copyright © 2020-2023  润新知