raise_application_error用法

时间:2021-12-26 14:59:18

我们经常通过dbms_output.put_line来输出异常信息,但有时需要把异常信息返回给调用的客户端。此时我们用raise_application_error,允许用户在pl/sql中返回用户自定义的“ORA-”错误代码和错误信息

raise_application_error语法:

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

error_number:允许-20000到-20999间的负数

message:允许最长2k的字符串

TRUE:the error is placed on the stack of previous errors.

FALSE:(the default), the error replaces all previous errors.

例子:Raising an Application Error With raise_application_error

DECLARE
   num_tables NUMBER;
BEGIN
   SELECT COUNT(*) INTO num_tables FROM USER_TABLES;
   IF num_tables < 1000 THEN
      raise_application_error(-20101, 'Expecting at least 1000 tables');
   ELSE
      NULL; -- Do the rest of the processing (for the non-error case).
   END IF;
END;
/