【问题标题】:How to create update trigger如何创建更新触发器
【发布时间】:2014-01-21 11:50:23
【问题描述】:

我正在尝试创建一个触发器,该触发器将在订单表中输入项目时运行,产品表中的数量应该减少

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'

任何可以帮助解决为什么会出现这些错误的帮助都会有所帮助

【问题讨论】:

  • 表中产品没有O_ID字段

标签: sql oracle triggers transactions


【解决方案1】:

我想一定是这个:

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

必须在餐桌订单而不是餐桌产品上定义触发器。

顺便说一句,如果下订单(即插入订单)会发生什么。我认为这也应该更新产品数量。

【讨论】:

  • 是的,触发器应该更新两个表,减少产品数量
【解决方案2】:

你的错误在这里

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 了解更多信息。

【讨论】:

    猜你喜欢
    • 2019-05-03
    • 1970-01-01
    • 2012-07-31
    • 2016-05-26
    • 2017-03-27
    • 2016-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多