插入或更新销售项目后 oracle 触发器

发布于 2024-08-18 13:24:53 字数 956 浏览 6 评论 0原文

我有这个表,它代表一个弱实体,是一个用于介绍订购项目的典型表: 插入 ITEM_FORNECIMENTO 值(a_orderId、a_prodId、a_prodQtd、a_buyPrice);

我希望我的触发器更新最后一列(没有 iva 的产品总价)来执行以下操作:totalPrice=totalPrice*(1+(iva/100),每次我在该表上插入或更新条目时。

所以,我想出了这个,但在使用新值和旧值时我完全错误。

create or replace
trigger t_replaceTotal
after insert or update of id_prod,qtd_if,prec_total_if on item_fornecimento
for each row 

declare
iva produto.iva_prod%type;
idProd  produto.id_prod%type;
r_old item_fornecimento.prec_total_if%type:=null;
r_new item_fornecimento.prec_total_if%type:=null;

begin
  select iva_prod,id_prod into iva,idprod from produto p where p.id_prod = id_prod; 
  r_old:= :old.prec_total_if;
  r_new:= :new.prec_total_if;
  update item_fornecimento item set prec_total_if = r_old * (1+(iva/100)) where item.id_prod = idprod;
end;

有人可以帮助我重写此代码吗?ORA-01422:精确获取返回的行数超过了请求的行数。 ORA-06512: 在“FUSION.T_REPLACETOTAL”,第 8 行 ORA-04088: 执行触发器“FUSION.T_REPLACETOTAL”期间出错

I have this table that represents a weak entity and is a typical table for introducing items ordered:
insert into ITEM_FORNECIMENTO values (a_orderId,a_prodId,a_prodQtd, a_buyPrice);

I want my trigger to update the last column (the total price of products WITHOUT iva) to do this : totalPrice= totalPrice*(1+(iva/100), each time I insert or update an entry on that table.

so, I came up with this, but I'm totally wrong when it comes to work with new and old values.

create or replace
trigger t_replaceTotal
after insert or update of id_prod,qtd_if,prec_total_if on item_fornecimento
for each row 

declare
iva produto.iva_prod%type;
idProd  produto.id_prod%type;
r_old item_fornecimento.prec_total_if%type:=null;
r_new item_fornecimento.prec_total_if%type:=null;

begin
  select iva_prod,id_prod into iva,idprod from produto p where p.id_prod = id_prod; 
  r_old:= :old.prec_total_if;
  r_new:= :new.prec_total_if;
  update item_fornecimento item set prec_total_if = r_old * (1+(iva/100)) where item.id_prod = idprod;
end;

Could someone please help rewriting this code? I'm getting the error: ORA-01422: exact fetch returns more than requested number of rows
ORA-06512: at "FUSION.T_REPLACETOTAL", line 8
ORA-04088: error during execution of trigger 'FUSION.T_REPLACETOTAL'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

守不住的情 2024-08-25 13:24:53

问题是您试图从表中获取每一行,因为您正在将列 (id_prod) 与其自身匹配。我怀疑您想使用 :new.id_prod 或 :old.id_prod。

select .... from produto p where p.id_prod = id_prod; 

接下来,将其设置为 BEFORE INSERT/UPDATE 触发器,并将 UPDATE 语句替换为

 :new.prec_total_if := r_old * (1+(iva/100));

否则,您将遇到混乱的变异表错误。

The problem is that you are trying to fetch every row from the table because you are matching a column (id_prod) to itself. I suspect you want to use :new.id_prod or :old.id_prod.

select .... from produto p where p.id_prod = id_prod; 

Next, make this a BEFORE INSERT/UPDATE trigger and replace the UPDATE statement with

 :new.prec_total_if := r_old * (1+(iva/100));

Otherwise you'll get a mess of mutating table errors.

愿得七秒忆 2024-08-25 13:24:53

该错误告诉您将结果保存到变量中的查询返回多个结果。因此它不知道你想在变量中保存什么。

尝试运行以下命令:

select iva_prod,id_prod from produto p where p.id_prod = id_prod;

我敢打赌它会给你多个结果,但它无法保存到 iva,idprod 中。

That error is telling you that a query that saves its result into a variable is returning more than one result. As such it doesn't know what you want to save in the variable.

Try running the following:

select iva_prod,id_prod from produto p where p.id_prod = id_prod;

and I bet it will give you more than one result, which it can't save into iva,idprod.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文