zoukankan      html  css  js  c++  java
  • msql 实现sequence功能增强

    create table sequence (  
        seq_name        VARCHAR(50)  NOT NULL COMMENT '序列名称',
        min_val         INT  UNSIGNED         NOT NULL COMMENT '最小值',
        max_val         INT  UNSIGNED         NOT NULL COMMENT '最大值',
        if_cycle        INT  UNSIGNED         NOT NULL COMMENT '是否循环',
        if_use          INT  UNSIGNED         NOT NULL COMMENT '是否使用中:0:未使用,1:已使用',
        current_val     INT  UNSIGNED         NOT NULL COMMENT '当前值',
        increment_val   INT  UNSIGNED         NOT NULL DEFAULT 1 COMMENT '步长(跨度)',
        PRIMARY KEY (seq_name)
    );

    delimiter $$

    create function currval(v_seq_name VARCHAR(50))  
    returns integer
    begin
        declare v_curr_val integer;  
        set v_curr_val := 0;  
        select current_val into v_curr_val  
        from sequence
        where seq_name = v_seq_name;  
        return v_curr_val;  
    end$$

    create function nextval(v_seq_name VARCHAR(50))
    returns INTEGER
    begin
        declare v_curr_val INTEGER;
        declare v_increment_val INTEGER;
        declare v_if_cycle INTEGER;
        declare v_next_val INTEGER;
        declare v_min_val INTEGER;
        declare v_max_val INTEGER;

        update sequence set if_use = 1 where seq_name = v_seq_name;
        select current_val, min_val, max_val, increment_val, if_cycle
        into v_curr_val, v_min_val, v_max_val, v_increment_val, v_if_cycle from sequence where seq_name = v_seq_name;
        set v_next_val := v_curr_val + v_increment_val;
        if (v_next_val > v_max_val) and (v_if_cycle = 0) then
            set v_next_val := -1;
        else
            set v_next_val := mod(v_next_val, v_max_val + 1);
            IF v_next_val < v_min_val then
                set v_next_val := v_min_val;
            end if;
            update sequence set current_val = v_next_val where seq_name = v_seq_name;   
        end if;  
        update sequence set if_use = 0 where seq_name = v_seq_name;   
        return v_next_val;
    end$$

    create function setval(v_seq_name VARCHAR(50), v_new_val INTEGER)  
    returns integer
    begin
        declare v_min_val int;
        declare v_max_val int;
        update sequence set if_use = 1 where seq_name = v_seq_name;    
        select min_val, max_val into v_min_val, v_max_val from sequence where seq_name = v_seq_name;
        if (v_new_val > v_max_val) or (v_new_val < v_min_val) then
            set v_new_val := -1;
        else
            update sequence set current_val = v_new_val where seq_name = v_seq_name;
        end if;
        update sequence set if_use = 0 where seq_name = v_seq_name;
        return v_new_val;
    end$$

    delimiter ;

  • 相关阅读:
    ASP.NET 判断GRIDVIEW的checkbox是否选中
    分享C#实现XML和实体序列化和反序列化的代码
    设计模式:简单工厂、工厂方法、抽象工厂之小结与区别 (转)
    如何验证已经加载的symbol file与module是否匹配?
    成功运行过的WinDBG Commands–12262010
    间歇性连接数据库失败, 先试试下面两篇文章
    如何使用符号文件?
    为<<Advanced Windows Debugging>>配置符号路径
    TCP中Connection和端口的关系
    SQL Profiler Trace中的列SPID
  • 原文地址:https://www.cnblogs.com/xtly2012/p/4643981.html
Copyright © 2011-2022 走看看