EXECUTE IMMEDIATE仅在有时工作

时间:2018-01-26 14:13:08

标签: sql oracle plsql

我目前有这个plsql块来删除并创建一个序列:

BEGIN 
  EXECUTE IMMEDIATE 'DROP SEQUENCE my_sequence';

  EXECUTE IMMEDIATE 'CREATE SEQUENCE my_sequence START WITH 1 INCREMENT BY 1 ORDER CYCLE MAXVALUE 999';

  EXCEPTION
  WHEN OTHERS THEN
      IF SQLCODE = -2289 THEN
       EXECUTE IMMEDIATE 'CREATE SEQUENCE my_sequence START WITH 1 INCREMENT BY 1 ORDER CYCLE MAXVALUE 999';
      ELSE
        RAISE;
  END IF;

END;
/

运行此命令后,通常会创建序列。但有时它不是。

有人知道为什么吗?

由于

1 个答案:

答案 0 :(得分:1)

我无法想象您需要重置CYCLE序列的任何情况。无论如何,我会这样做:

BEGIN 
    FOR aSeq IN (SELECT * FROM USER_SEQUENCES WHERE SEQUENCE_NAME = 'MY_SEQUENCE') LOOP
        EXECUTE IMMEDIATE 'DROP SEQUENCE '||aSeq.SEQUENCE_NAME;
    END LOOP;

    EXECUTE IMMEDIATE 'CREATE SEQUENCE MY_SEQUENCE START WITH 1 INCREMENT BY 1 ORDER CYCLE MAXVALUE 999';
END;
/