2014-01-21 169 views
0

我試圖創建項目時在訂單表都進入了產品表中的數量是假設減少如何創建更新觸發器

create or replace trigger quantity_dicr after update of quantity on products for each row 
declare 
    quantity float; 
    begin 
     select quantity into quantity from products where o_id = :new.o_id; 
     set quantity = quantity + :new.quantity , 
     where o_id= :new.o_id; 
    end quantity_dicr; 

表,將運行觸發器

create table products(
prod_id numeric not null, 
prod_name varchar2(50) not null, 
quantity numeric not null, 
price numeric not null, 
constraint prod_id_pk primary key(prod_id) 
) 
create table orders 
(
prod_id numeric not null, 
o_id numeric not null, 
quantity numeric not null, 
o_sum numeric not null, 
constraint fk_products foreign key (prod_id) references products(prod_id), 
constraint orders_pk primary key (o_id)) 
) 

它給了我這些錯誤

Error(4,64): PLS-00049: bad bind variable 'NEW.O_ID' 
Error(5,7): PL/SQL: SQL Statement ignored 
Error(5,11): PL/SQL: ORA-00922: missing or invalid option 
Error(6,19): PLS-00049: bad bind variable 'NEW.O_ID' 

任何可以遮光的幫助爲什麼有這些錯誤將有助於全

+0

表中產品沒有字段O_ID –

回答

0

我想那一定是這一個:

create or replace trigger quantity_dicr 
    after update of quantity on orders 
    for each row 

觸發必須在訂單表,而不是表的產品進行定義。

順便說一句,如果訂單(即插入訂單)會發生什麼情況。我認爲這也應該更新產品數量。

+0

是觸發器應更新兩個表,減少產品數量 –

0

你的錯誤是在這裏

create or replace trigger quantity_dicr after update of quantity on products for each row 
declare 
    quantity float; 
begin 
    select quantity into quantity from products where o_id = :new.o_id; 
    set quantity = quantity + :new.quantity , 
    where o_id= :new.o_id; --Don't have field O_ID in table products 
end quantity_dicr; 

你可以做到這樣。

CREATE OR REPLACE TRIGGER quantity_dicr AFTER UPDATE ON orders FOR EACH ROW 
    UPDATE products SET quantity = quantity - :NEW.quantity WHERE prod_id = :NEW.prod_id; 
END quantity_dicr; 

閱讀本questionofficial documentation以獲取更多信息。