带有两个表的变量的SQL触发器

时间:2011-12-11 22:32:49

标签: exception plsql triggers

以下是两个表的查询:

SQL> describe order_line;
Name                                                                    Null?    Type
----------------------------------------------------------------------- -------- ------------------
O_ID                                                                    NOT NULL NUMBER(8)
INV_ID                                                                  NOT NULL NUMBER(10)
OL_QUANTITY                                                             NOT NULL NUMBER(4)

SQL> describe inventory; 

Name                                                                    Null?    Type
----------------------------------------------------------------------- -------- ------------------
INV_ID                                                                  NOT NULL NUMBER(10)
ITEM_ID                                                                          NUMBER(8)
COLOR                                                                            VARCHAR2(20)
INV_SIZE                                                                         VARCHAR2(10)
INV_PRICE                                                                        NUMBER(6,2)
INV_QOH                                                                          NUMBER(4)

如果库存中相应的inv_qoh小于100,我试图在order_line中删除行时触发触发器。这是我的代码到目前为止:

create or replace trigger ol_bef_del
before delete on order_line for each row
declare
    inv_inv_id number;
    inv_inv_qoh number;
    little_inv_error exception;
begin
    select inv_id, inv_qoh into inv_inv_id, inv_inv_qoh from inventory
    where inv_inv_id = inv_id;
if inv_inv_qoh < 100
raise little_inv_error;
end if;
exception
when little_inv_error then
    raise_application_error (-20002,
    'Wait a minute - too little in Inventory');
end;
/

我做错了什么?

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

您需要使用:OLD来访问要删除的行的值,如下所示:

select inv_id, inv_qoh into inv_inv_id, inv_inv_qoh from inventory
where inv_inv_id = :old.inv_id;
--                 ^^^^