如何在oracle中使用单个过程重置所有序列

eaf3rand  于 2023-03-29  发布在  Oracle
关注(0)|答案(1)|浏览(133)

我们有一个表与Map表和序列我们如何重置所有序列相应的以下Map与最大计数使用一个过程或someting..?

bvjveswy

bvjveswy1#

这是Scott的示例模式。

SQL> create sequence seq_employee;

Sequence created.

SQL> create sequence seq_dept;

Sequence created.

SQL> select seq_employee.nextval, seq_dept.nextval from dual;

   NEXTVAL    NEXTVAL
---------- ----------
         1          1

SQL> select count(*) from emp;

  COUNT(*)
----------
        14

SQL> select count(*) from dept;

  COUNT(*)
----------
         4

SQL>

游标FOR循环从TAB_SEQ_MAPPING表中选择(我用UNION模拟了它)。

SQL> set serveroutput on
SQL>
SQL> declare
  2    l_val number;
  3    l_cnt number;
  4    l_str varchar2(200);
  5  begin
  6    for cur_r in (select 'emp' table_name, 'seq_employee' sequence_name from dual union all
  7                  select 'dept'          , 'seq_dept'                   from dual
  8                 )
  9    loop
 10      l_str := 'select count(*) from ' || cur_r.table_name;
 11      dbms_output.put_Line(l_str);
 12      execute immediate l_str into l_cnt;
 13
 14      l_str := 'select ' || cur_r.sequence_name ||'.nextval from dual';
 15      dbms_output.put_Line(l_str);
 16      execute immediate l_str into l_val;
 17
 18      l_str := 'alter sequence ' || cur_r.sequence_name || ' increment by ' || (l_cnt - l_val);
 19      dbms_output.put_Line(l_str);
 20      execute immediate l_str;
 21
 22      l_str := 'select ' || cur_r.sequence_name ||'.nextval from dual';
 23      dbms_output.put_Line(l_str);
 24      execute immediate l_str into l_val;
 25
 26      l_str := 'alter sequence ' || cur_r.sequence_name || ' increment by 1';
 27      dbms_output.put_Line(l_str);
 28      execute immediate l_str;
 29    end loop;
 30  end;
 31  /
select count(*) from emp
select seq_employee.nextval from dual
alter sequence seq_employee increment by 12
select seq_employee.nextval from dual
alter sequence seq_employee increment by 1
select count(*) from dept
select seq_dept.nextval from dual
alter sequence seq_dept increment by 2
select seq_dept.nextval from dual
alter sequence seq_dept increment by 1

PL/SQL procedure successfully completed.

SQL>

一旦结束:

SQL> select seq_employee.nextval, seq_dept.nextval from dual;

   NEXTVAL    NEXTVAL
---------- ----------
        15          5

SQL>

相关问题