DB2在午夜重置序列

ezykj2lf  于 2022-11-07  发布在  DB2
关注(0)|答案(1)|浏览(276)

所以我试着在每天午夜把一个序列重置为o,这是我有的一个程序,但我迷路了。

CREATE OR REPLACE PROCEDURE my_seq_reset AS
 BEGIN
EXECUTE IMMEDIATE 'DROP SEQUENCE BRGSEQ';
EXECUTE IMMEDIATE
  'CREATE SEQUENCE BRGSEQ' ||
  '  MINVALUE 1 ' ||
  '  MAXVALUE 999999 ' ||
  '  START WITH 1 ' ||
  '  INCREMENT BY 1 ' ||
  '  NOCACHE';

结束;

qrjkbowd

qrjkbowd1#

您可以使用Db2 administrative task scheduler(ATS)来执行此操作。请从Db2命令行处理器在服务器上启用它:

db2set DB2_ATS_ENABLE=YES

运行以下一组语句。

--#SET TERMINATOR @

-- create a sequence
CREATE SEQUENCE DB2ADMIN.BRGSEQ 
MINVALUE 1
MAXVALUE 999999
START WITH 1
INCREMENT BY 1
NOCACHE
@

-- create a routine for Db2 ATS
CREATE OR REPLACE PROCEDURE DB2ADMIN.my_seq_reset
BEGIN
  EXECUTE IMMEDIATE 'ALTER SEQUENCE DB2ADMIN.BRGSEQ RESTART WITH 1';
END@

-- schedule the routine to run at midnight every day
CALL SYSPROC.ADMIN_TASK_ADD
(
  'DB2ADMIN.BRGSEQ sequence reset'
, CURRENT_TIMESTAMP
, NULL
, NULL
, '0 0 * * *'
, 'DB2ADMIN'
, 'MY_SEQ_RESET'
, NULL
, NULL
, NULL
)@

相关问题