oracle 儲存過程 每天凌晨 更新sequence 從0 開始
阿新 • • 發佈:2019-01-11
CREATE OR REPLACE PROCEDURE seq_reset AS
n NUMBER(10);
tsql VARCHAR2(100);
BEGIN
EXECUTE IMMEDIATE 'select SQD_LSH.nextval from dual'
INTO n;
n := - (n );
tsql := 'alter sequence SQD_LSH increment by ' || n;
EXECUTE IMMEDIATE tsql;
EXECUTE IMMEDIATE 'select SQD_LSH.nextval from dual'
INTO n;
tsql := 'alter sequence SQD_LSH increment by 1';
EXECUTE IMMEDIATE tsql;
n NUMBER(10);
tsql VARCHAR2(100);
BEGIN
EXECUTE IMMEDIATE 'select SQD_LSH.nextval from dual'
INTO n;
n := - (n );
tsql := 'alter sequence SQD_LSH increment by ' || n;
EXECUTE IMMEDIATE tsql;
EXECUTE IMMEDIATE 'select SQD_LSH.nextval from dual'
INTO n;
tsql := 'alter sequence SQD_LSH increment by 1';
EXECUTE IMMEDIATE tsql;
END seq_reset;
先得到sequence的當前值,然後設定其步長為這個值的相反數,在執行nextval相當於減去已經增長的部分,使sequence重新歸零,最後恢復步長為1.
然後 建立一個 job 設定成每天執行這個 儲存過程即可