2011-01-27 59 views

回答

4

IF EXISTS子句在Oracle中的DROP SEQUENCE命令中不存在。

你可以使用一個PLSQL塊忽略錯誤:

SQL> DECLARE 
    2  sequence_doesnt_exist EXCEPTION; 
    3  PRAGMA EXCEPTION_INIT(sequence_doesnt_exist, -2289); 
    4 BEGIN 
    5  EXECUTE IMMEDIATE 'DROP SEQUENCE seq_name'; 
    6 EXCEPTION 
    7  WHEN sequence_doesnt_exist THEN NULL; 
    8 END; 
    9/

PL/SQL procedure successfully completed 
+0

喔,好... ...我怎樣才能檢查是否存在的序列或不基於我可以放棄它。 – CodeRocker 2011-01-27 18:19:22

+0

謝謝文森特:) – CodeRocker 2011-01-27 18:29:48

0

的問題是「是否存在」不甲骨文工作。使用:

drop sequence ownername.seq_name; 
1

正如其他人提到的那樣,IF EXISTS對DROP SEQUENCE命令不起作用。

要測試序列的存在,你需要檢查相應的視圖:

USER_SEQUENCES

SELECT * 
    FROM USER_SEQUENCES 
WHERE sequence_name = ? 

DBA_SEQUENCES

SELECT * 
    FROM DBA_SEQUENCES 
WHERE sequence_name = ? 

ALL_SEQUENCES

SELECT * 
    FROM ALL_SEQUENCES 
WHERE sequence_name = ? 

例子:

BEGIN 
    FOR i IN (SELECT sequence_name 
       FROM USER_SEQUENCES 
       WHERE sequence_name = ?) 
    LOOP 
    EXECUTE IMMEDIATE ('DROP SEQUENCE '|| i.sequence_name); 
    END LOOP; 
END; 
0

試試這個:

DECLARE 
iNum NUMBER DEFAULT 0; 
BEGIN 
    SELECT COUNT(1) 
     INTO iNum 
    FROM ALL_SEQUENCES 
    WHERE SEQUENCE_OWNER='<OWNER_NAME>' 
     AND SEQUENCE_NAME = '<YOUR_SEQUENCE_NAME>'; 
IF iNum> 0 THEN 
    EXECUTE IMMEDIATE 'DROP SEQUENCE <OWNER_NAME>.<YOUR_SEQUENCE_NAME>'; 
END IF; 
END; 
相關問題