我想捕获sequence.nextval
在Oracle中达到MAXVALUE时抛出的特定异常?
我不想在别人的时候使用EXCEPTION;我想捕获记录器的特定异常。
答案 0 :(得分:4)
您可以使用-8004
声明用户定义的异常并将异常代码pragma exception_init()
(当序列超过其最大值时引发的异常代码)与它关联,然后捕获该特定异常。这是一个例子:
SQL> declare
2 l_ex_max_val exception;
3 pragma exception_init(l_ex_max_val, -8004);
4 l_val number;
5 begin
6 l_val := gen_val.nextval; -- This kind of assignment is allowed
7 exception -- in 11g and further versions. In earlier versions
8 when l_ex_max_val -- you would use select statement
9 then dbms_output.put_line('ERROR! sequence gen_val exceeds its max value');
10 end;
11 /
ERROR! sequence gen_val exceeds its max value
PL/SQL procedure successfully completed
SQL>