我尝试通过 SQL*Plus 运行以下 PL/SQL 脚本:
DECLARE
table_exists number;
sequence_exists number;
sequence_start number;
BEGIN
select count(*) into sequence_exists
from all_sequences
where sequence_name='DBSEQ';
select count(*) into table_exists
from dba_tables
where table_name='DBTABLE';
IF sequence_exists = 0 AND table_exists > 0 THEN
select MAX(ID) + 1 into sequence_start from DBTABLE;
ELSE
sequence_start := 1;
END;
IF sequence_exists = 0 THEN
execute immediate 'CREATE SEQUENCE DBSEQ
start with ' || sequence_start || '
increment by 1
nomaxvalue';
END IF;
END;
(这有点最小化,以显示实际失败的部分)。
问题是该行
select MAX(ID) + 1 into sequence_start from DBTABLE;
因“PL/SQL:ORA-00942:表或视图不存在”而失败,因为表不存在(并且我已经验证 table_exists 变量实际上是 0)。所以基本上,即使 if 子句不执行,选择也会运行(或至少失败)。我还验证了 if 子句确实失败了,方法是将 select 替换为创建一个表,该表在运行脚本后不存在。
我也尝试过捕获异常,做这样的事情:
BEGIN
select MAX(ID) + 1 into sequence_start from DBTABLE;
EXCEPTION
WHEN OTHERS THEN
sequence_start := 1;
END;
但这产生了同样的错误。
那么,select 语句有什么特别之处,使它先于其他任何东西运行吗?我应该如何解决我的问题?