日期:2014-05-16  浏览次数:20526 次

每天凌晨清空sequence,让sequence每天从1开始

job:

?

begin
  sys.dbms_job.submit(job => :job,
                      what => 'clearseq(''SEQ_SN_ID'');',
                      next_date => to_date('16-10-2012 18:37:20', 'dd-mm-yyyy hh24:mi:ss'),
                      interval => 'trunc(SYSDATE)+1/24+1');
  commit;
end;
/

?

存储过程删除sequence,然后新建sequence,

但要注意,此方法对存储过程掉sequence不能使用,因为删掉一个sequence后再新建后,存储过程要重新编译下才能

使用。

此方法仅给java取sequence使用

create or replace procedure ClearSeq(seqName in varchar) as
  n_count number(8);
  v_sql   varchar(500);
  v_code  NUMBER;
  v_errm  VARCHAR2(64);
  --mahongmin
begin
  select count(1)
    into n_count
    from user_sequences t
  --where t.sequence_name = 'SEQ_SN_ID';
   where t.sequence_name = seqName;

  if n_count > 0 then
    execute immediate 'drop sequence ' || seqName;
  end if;

  v_sql := 'create sequence ' || seqName ||
           ' minvalue 1 maxvalue 99999999 start with 1 increment by 1 NOCYCLE cache 20';
  execute immediate v_sql;
exception
  when others then
    v_code := SQLCODE;
    v_errm := SUBSTR(SQLERRM, 1, 64);
    DBMS_OUTPUT.PUT_LINE('重置sequence时, Error code ' || v_code || ': ' ||
                         v_errm);
end;