应使用哪个 Raise 应用程序错误号

which Raise application error number should be used

我正在为我的数据库创建触发器。在我的触发器代码中,我应该使用哪个数字参数:

set serveroutput on

create or replace trigger iss_bk 
  after insert on Issue 
  for each row
declare
  p integer;
  q integer;
begin
  select Available_copy 
    into p 
    from book 
    where Book.ISBN = :NEW.book_id;

  q := p - :NEW.quantity;

  if q < 0 then
     RAISE_APPLICATION_ERROR(-1722,' Exceeded available quantity of book .');
  else
     update Book set 
       Available_copy = q 
       where Book.ISBN = :NEW.book_id;
  end if;    
end;
/

虽然 q < 0 input is given, the error is :

ORA-21000: error number argument to raise_application_error of -1722 is out of range
ORA-06512: at "R1507090.ISS_BK", line 9

根据 documentation(如果需要,请选择另一个版本,但我认为变化不大)。 "Handling PL/SQL Errors" 说:

raise_application_error(error_number, message[, {TRUE | FALSE}]);

where

  • error_number is a negative integer in the range -20000 .. -20999
  • message is a character string up to 2048 bytes long.
  • If the optional third parameter is TRUE, the error is placed on the stack of previous errors. If the parameter is FALSE (the default), the error replaces all previous errors.

这意味着你应该使用,例如,

RAISE_APPLICATION_ERROR(-20001, 'Exceeded available quantity of book.');

可以重复使用相同的 ERROR_NUMBER 值,即 所有 您自己的错误消息可以共享相同的“-20001”代码,如果您愿意的话。