触发警告消息

时间:2018-12-02 00:43:15

标签: database oracle plsql

当涉及到SQL时,我还很新,我目前正在处理触发器,但是我有一个触发器,我不确定问题是什么。我想知道是否有人可以帮助我找出问题所在。说实话,我什至不确定这是否是一个好的触发条件。我和他们有一点麻烦。谢谢。我很感激。

触发以引发关于新购买或更新购买的错误

CREATE OR REPLACE TRIGGER reminder1
AFTER INSERT OR UPDATE ON PurchasedDeal
FOR EACH ROW
BEGIN
RAISE_APPLICATION_ERROR( 'Notify new Purchased Deal Created or updated' );
END;

它的全部意思是:

警告:触发器因编译错误而创建。

1 个答案:

答案 0 :(得分:0)

不确定用于开发PL / SQL代码的软件。但是,您应该了解如何使“编译错误”消息可见,例如

使用sqlcl(命令行工具)

-- just a test table ...
create table purchaseddeal ( id )
as
select 1 from dual ;


CREATE OR REPLACE TRIGGER reminder1
AFTER INSERT OR UPDATE ON PurchasedDeal
FOR EACH ROW
BEGIN
RAISE_APPLICATION_ERROR( 'Notify new Purchased Deal Created or updated' );
END;
/

SQL> show error

Errors for TRIGGER ...REMINDER1:

LINE/COL ERROR
-------- --------------------------------------------------------------------------------------------
2/1      PL/SQL: Statement ignored
2/1      PLS-00306: wrong number or types of arguments in call to 'RAISE_APPLICATION_ERROR'

使用https://livesql.oracle.com时您会得到

-- this will be displayed straightaway after compiling ...
Errors: TRIGGER REMINDER1 
Line/Col: 2/1 PL/SQL: Statement ignored 
Line/Col: 2/1 PLS-00306: wrong number or types of arguments in call to 'RAISE_APPLICATION_ERROR'

然后,您可以查看documentation了解更多信息。

  

RAISE_APPLICATION_ERROR过程

     

您可以调用RAISE_APPLICATION_ERROR过程(在   DBMS_STANDARD包)仅来自存储的子程序或方法。   通常,您调用此过程来引发用户定义的异常   并将其错误代码和错误消息返回给调用者。

     

要调用RAISE_APPLICATION_ERROR,请使用以下语法:

     

RAISE_APPLICATION_ERROR(错误代码,消息[,{TRUE | FALSE}]);您   必须已将error_code分配给用户定义的异常,并且   EXCEPTION_INIT编译指示。语法为:

     

PRAGMA EXCEPTION_INIT(异常名称,错误代码)错误代码为   范围为-20000 ..- 20999的整数,并且消息为字符   最多2048个字节的字符串。

(对于您自己或其他程序员)可能不明显(为什么对您自己或其他程序员而言),当事情按计划进行时,即当成功插入或更新一行时,将使用名为RAISE_APPLICATION_ERROR的过程。也许以下示例可以更好地说明您要编写(或查看)的内容:

表格:购买交易

SQL> select * from purchaseddeal ;

PRODUCT_ID        QTY      PRICE
---------- ---------- ----------
         1         10        100

触发

-- fires when a price is too high or too low 
-- (this could also be coded as a CHECK constraint - but the question is about triggers ...).

create or replace trigger checktheprice
  before insert or update on purchaseddeal
  for each row
begin
  if :new.price < 0 then
    raise_application_error( -20000, 'Price too low' ) ;
  elsif :new.price > 1000 then
    raise_application_error( -20001, 'Price too high' ) ;
  end if;
end ;
/

测试

SQL> insert into purchaseddeal values ( 2, 20, 2000 ) ;

Error starting at line : 1 in command -
insert into purchaseddeal values ( 2, 20, 2000 )
Error report -
ORA-20001: Price too high
ORA-06512: at "...CHECKTHEPRICE", line 5
ORA-04088: error during execution of trigger '...CHECKTHEPRICE'

SQL> insert into purchaseddeal values ( 2, 20, -2000 ) ;

Error starting at line : 1 in command -
insert into purchaseddeal values ( 2, 20, -2000 )
Error report -
ORA-20000: Price too low
ORA-06512: at "...CHECKTHEPRICE", line 3
ORA-04088: error during execution of trigger '...CHECKTHEPRICE'

表格仍未更改...

select * from purchaseddeal ;

SQL> select * from purchaseddeal ;

PRODUCT_ID        QTY      PRICE
---------- ---------- ----------
         1         10        100

匿名块和PRAGMA EXCEPTION_INIT

declare
  price_too_low  exception ; 
  price_too_high   exception ;
-- assign the error_codes to the user-defined exceptions
  pragma exception_init( price_too_low, -20000 ) ;
  pragma exception_init( price_too_high, -20001 ) ;
begin
  insert into purchaseddeal values ( 2, 20, 2000 ) ;
--  insert into purchaseddeal values ( 2, 20, -2000 ) ;
exception 
  when price_too_low then 
    dbms_output.put_line( to_char( sqlerrm( -20000 ) ) ) ;
  when price_too_high then
    dbms_output.put_line( to_char( sqlerrm( -20001 ) ) ) ;
end ;
/ 

-- output
ORA-20001: Price too high
ORA-06512: at "...CHECKTHEPRICE", line 5
ORA-04088: error during execution of trigger '...CHECKTHEPRICE'

PL/SQL procedure successfully completed.

更多测试:值在允许范围内

-- insert some rows that contain values in the allowed range
insert into purchaseddeal ( product_id, qty, price )
select 
  level + 1, ( level + 1 ) * 10, ( level + 1 ) * 100.00
from dual
connect by level <= 8 ;

该表现在包含...

SQL> select * from purchaseddeal ;

PRODUCT_ID        QTY      PRICE
---------- ---------- ----------
         1         10        100
         2         20        200
         3         30        300
         4         40        400
         5         50        500
         6         60        600
         7         70        700
         8         80        800
         9         90        900

9 rows selected.