RAISE_APPLICATION_ERROR 不返回消息 (RAISE_APPLICATION_ERROR doesn't return the message)


問題描述

RAISE_APPLICATION_ERROR 不返回消息 (RAISE_APPLICATION_ERROR doesn't return the message)

IF l_value = 'FALSE' THEN
  RAISE_APPLICATION_ERROR(-20299, 'some error message');
END IF;

This is part of table trigger. It should return me a error number and message, but when alert pops out it returns only message number. No 'some error message'. Whats wrong


參考解法

方法 1:

Maybe the name RAISE_APPLICATION_ERROR is misleading for you. It will not pop up something onto your GUI. That you program yourself depending on what client you are using. Put you can use RAISE_APPLICATION_ERROR to create your own SQL errors on which you act upon.

Example

-- a example table
create table mytest (col_a number, col_b char(20));

-- a example trigger
CREATE OR REPLACE TRIGGER mytest_before
BEFORE UPDATE
    ON mytest
    FOR EACH ROW
DECLARE
BEGIN
    if :new.col_a < 0 then
      RAISE_APPLICATION_ERROR(-20299, 'negative value not allowed for column A');
    end if;
END;

insert into mytest values (1,'hallo');

set serveroutput on
DECLARE
  negative_value EXCEPTION; -- declare exception
  PRAGMA EXCEPTION_INIT (negative_value, -20299); -- assign error code to exception
BEGIN
  update mytest set col_a = -1 where col_b = 'hallo';
EXCEPTION
  WHEN negative_value THEN -- handle exception
    -- do whatever you need to do to bring the error to the user
    DBMS_OUTPUT.PUT_LINE(TO_CHAR(SQLERRM(-20299)));
END;
/

The above will bring you the output in SQL*Plus or SQL Developer of that sort. 

table MYTEST created.
TRIGGER mytest_before compiled
1 rows inserted.
anonymous block completed
ORA-20299: negative value not allowed for column A
ORA-06512: at "DEMO.MYTEST_BEFORE", line 4
ORA-04088: error during execution of trigger 'DEMO.MYTEST_BEFORE

Instead of DBMS_OUTPUT.PUT_LINE you can do whatever you need to do to show the user whatever you want him to show.

方法 2:

The alert in your form has been raised by some trigger code on your form. Have a look at your ON-ERROR trigger - what code does it have?

You may need to augment it to show DBMS_ERROR_TEXT in the alert.

(by DARK_AholJeffrey Kemp)

參考文件

  1. RAISE_APPLICATION_ERROR doesn't return the message (CC BY-SA 3.0/4.0)

#plsql #raise #oracle #oracleforms






相關問題

RAISE_APPLICATION_ERROR 不返回消息 (RAISE_APPLICATION_ERROR doesn't return the message)

PL/SQL 塊和循環練習 (PL/SQL block and LOOP exercise)

如何從列中僅提取編號的行 (How to extract only numbered rows from a column)

如何編寫一個程序來自動執行一組查詢 (How to write a procedure to execute set of queries automatically)

使用 DBMS_OUTPUT.put_line 顯示錯誤消息 (Display error message using DBMS_OUTPUT.put_line)

如何編寫將Oracle數據庫表數據導出到excel文件的程序 (How to write a Procedure which exports Oracle database table data into excel file)

如何重載對像類型中的方法 (How to overload a method in an object type)

有沒有辦法以編程方式從 Oracle 包中提取表引用? (Is there a way to programmatically extract table references from an Oracle package?)

批量插入 Oracle 數據庫:哪個更好:FOR 游標循環還是簡單的選擇? (Bulk Insert into Oracle database: Which is better: FOR Cursor loop or a simple Select?)

如何使用參數“foo 表”執行 SP? (How to execute SP with arguments 'table of foo'?)

PL/SQL 我做錯了什麼? (PL/SQL What am I doing wrong?)

PL/SQL 顯示帶有條件的表中的數據 (PL/SQL Display Data From a Table with a Condition)







留言討論