代码改变世界

存储过程重置SEQUENCE值从新开始。

2015-07-20 14:00  夏洛克·福尔摩斯  阅读(577)  评论(0编辑  收藏  举报
CREATE OR REPLACE PROCEDURE RESET_SEQUENCE(
    v_SeqName     IN  VARCHAR2,
    v_sqlcode     OUT NUMBER,
    v_sqlerrm     OUT VARCHAR2)
IS
    Num           NUMBER(10);
    SQL_STR     VARCHAR2(100);
BEGIN
    v_sqlcode := 0;
    v_sqlerrm := 'successful';

    EXECUTE IMMEDIATE 'SELECT ' || v_SeqName || '.NEXTVAL FROM dual ' INTO Num;
    Num := - (Num);

    SQL_STR := 'alter sequence '|| v_SeqName ||' increment by ' || Num ;
    EXECUTE IMMEDIATE SQL_STR;

    EXECUTE IMMEDIATE 'SELECT ' || v_SeqName || '.NEXTVAL FROM dual ' INTO Num;
    SQL_STR := 'alter sequence '|| v_SeqName ||' increment by 1';
    EXECUTE IMMEDIATE SQL_STR;

EXCEPTION
  WHEN OTHERS THEN
    v_sqlcode := SQLCODE;
    v_sqlerrm := SQLERRM;
END;

注意空格,影响调用失败。