使用触发器更新表

时间:2014-04-18 10:50:52

标签: sql oracle triggers

每当product_inv更新但我的代码都没有运行时,我正在尝试更新产品表。请帮助我

create table Product
(           
       P_ID varchar2(10) constraint cat_pk PRIMARY KEY,
       P_name varchar2(15) constraint Cat_unqnn UNIQUE NOT NULL,
       P_Brand varchar2(15) CHECK(P_Brand IN('Yes', 'No')),
       P_Cost varchar2(6),
       P_qty number      
);

insert into product values('10',  'Coke', 'Yes', '34', '8');
insert into product values('20', 'Pepsi', 'No', '25', '6');


create table Product_Inv
(    
       P_ID varchar2(5) REFERENCES Product(P_ID),           
       Rate varchar2(5) constraint prodcut_rate_nn NOT NULL,           
       Qty number constraint prodcut_qty_nn NOT NULL,
       Discount varchar2(15),
       Lot_ExpiryDate varchar2(10)         
);

create or replace trigger Product
AFTER insert or update or delete on Product_Inv
for each row

begin
  UPDATE Product
       SET p_qty = p_qty + new.Qty 
       WHERE p_id = new.p_id; 

end;

在此插入之后,项目10将是之前的8 +这9,但是有一些错误。

 insert into Product_inv values('10', '25', '9', '25', '19-8-2014');

1 个答案:

答案 0 :(得分:0)

您的问题出现在触发器代码中: 你必须使用":"在新的面前。

所以代替这部分代码:

 UPDATE Product
       SET p_qty = p_qty + new.Qty 
       WHERE p_id = new.p_id; 

你需要把这部分放在一边:

 UPDATE Product
       SET p_qty = p_qty + :new.Qty 
       WHERE p_id = :new.p_id;