我在Oracle中遇到此错误:
ORA-00933:SQL命令未正确结束
对于
DROP SEQUENCE IF EXISTS ownername.seq_name;
为什么我会看到这个?
答案 0 :(得分:4)
Oracle中的DROP SEQUENCE
命令中不存在IF EXISTS
子句。
您可以使用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
答案 1 :(得分:1)
正如其他人所提到的,IF EXISTS不适用于DROP SEQUENCE命令。
要测试序列是否存在,您需要检查相应的视图:
SELECT *
FROM USER_SEQUENCES
WHERE sequence_name = ?
SELECT *
FROM DBA_SEQUENCES
WHERE sequence_name = ?
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;
答案 2 :(得分:0)
问题是“if exists”在Oracle中不起作用。使用:
drop sequence ownername.seq_name;
答案 3 :(得分: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;