简体   繁体   中英

exception sequence.nextval maxvalue? (oracle sql)

I want to catch the specific exception thrown when sequence.nextval reaches MAXVALUE in Oracle?

I don't want to use EXCEPTION WHEN OTHERS; I want to catch the specific exception for my logger.

You can declare a user defined exception and associate the exception code -8004 (the code of exception that is raised when a sequence exceeds its maximum value) with it using pragma exception_init() and then catch that specific exception. Here is an example:

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> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM