开发者

ORA-00933: SQL command not properly ended

开发者 https://www.devze.com 2023-02-07 06:40 出处:网络
I\'m getting this error in Oracle: ORA-00933: SQL command not properly ended for DROP SEQUENCE IF EXISTS ownername.seq_name;

I'm getting this error in Oracle:

ORA-00933: SQL command not properly ended

for

DROP SEQUENCE IF EXISTS ownername.seq_name;

开发者_开发百科

Why am I seeing this?


the IF EXISTS clause doesn't exist in the DROP SEQUENCE command in Oracle.

You could use a PLSQL block to ignore the error:

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


The problem is "if exists" does not work in Oracle. Use:

drop sequence ownername.seq_name;


As others mentioned, the IF EXISTS doesn't work on the DROP SEQUENCE command.

To test for the existence of a sequence, you need to check the appropriate view:

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 = ?

Example:

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


Try this:

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;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号