2011-12-11 86 views
0

這裏有兩個表的查詢:SQL觸發兩個表

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) 

我試圖讓一個觸發器ORDER_LINE刪除行的時候,如果在庫存相應inv_qoh是小於100火。這裏是我的代碼到目前爲止:

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; 
/

我在做什麼錯?

感謝您的幫助!

回答

0

您需要使用:OLD來訪問該行的值這樣被刪除:

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