修改序列当前值

  • Post author:
  • Post category:其他


可通过存储过程进行序列当前值的修改。

create OR REPLACE

procedure SETVAL

(

sch IN VARCHAR2, –模式名

tab IN VARCHAR2, –序列名

num IN NUMBER) –修改为什么值

AS

var1 NUMBER; –当前序列的下一个值

var2 NUMBER; –当前序列的下两个值

var3 NUMBER; –原序列步长

p_sql1 varchar2;

p_sql2 varchar2;

p_sql3 varchar2; –删除原来的序列

p_sql4 varchar2; –重新创建序列

dbms varchar2;

v_cursor1 varchar2;

begin

v_cursor1 := DBMS_SQL.OPEN_CURSOR;

p_sql1 := ‘SELECT ‘||sch||’.’||tab||’.NEXTVAL FROM dual;’;

execute immediate p_sql1 into var1;

p_sql2 := ‘SELECT ‘||sch||’.’||tab||’.NEXTVAL FROM dual;’;

execute immediate p_sql2 into var2;

–计算原步长

var3 := var2 – var1;

p_sql3 := ‘drop sequence ‘||sch||’.’||tab||’;’;

dbms_sql.parse(v_cursor1, p_sql3, dbms);

DBMS_SQL.EXECUTE(v_cursor1);

p_sql4 := ‘CREATE SEQUENCE ‘||sch||’.’||tab||’ INCREMENT BY ‘||var3||’ START WITH ‘||num||’ MAXVALUE 999999 MINVALUE 1 ORDER;’;

dbms_sql.parse(v_cursor1, p_sql4, dbms);

DBMS_SQL.EXECUTE(v_cursor1);

dbms_sql.close_cursor(v_cursor1);

    end;



版权声明:本文为weixin_47047820原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。