首页 > 解决方案 > ORA-04088: 执行触发器 'OES2.T_UPDATE_ORDERS_GROSS' 期间出错

问题描述

我创建了触发器: -- 删除触发器 t_update_orders_gross

create or replace trigger t_update_orders_gross
before insert on orders_update
for each row
declare
v_gross int;
v_subtotal int;
begin
select subtotal into v_subtotal from orders;
if v_subtotal is null then
    :new.subtotal:=testing(:new.order_no); 
--  testing is a function that will calculate the subtotal for the given 
-- order_no
    update orders set subtotal=:new.subtotal
    where order_no=:new.order_no;
end if;
end;

-- 在应该触发触发器的 orders_update 表中调用插入语句的过程:

create or replace procedure p_update_orders_grossSales
as
v_order_no orders.order_no%type;   
v_order_date orders.order_date%type;
v_ship_date orders.ship_date%TYPE;
v_shipping_method orders.shipping_method%type;
v_tax_status orders.tax_status%type;
v_subtotal orders.subtotal%type;
v_tax_amt orders.tax_amt%type;
v_shipping_charge orders.shipping_charge%type;
v_total_amt orders.total_amt%type;
v_customer_no orders.customer_no%type;
v_employee_no orders.employee_no%type;
v_branch_no orders.branch_no%type;
cursor c1 is select order_no,order_date,ship_date,shipping_method,tax_status, subtotal,tax_amt,shipping_charge,total_amt,customer_no,employee_no,branch_no from orders;

begin
open c1;
loop
    fetch c1 into v_order_no,v_order_date,v_ship_date,v_shipping_method,v_tax_status,v_subtotal,v_tax_amt,v_shipping_charge,v_total_amt,v_customer_no,v_employee_no,v_branch_no;
    exit when c1%notfound;
    insert into orders_update (
            order_no,order_date,ship_date,shipping_method,tax_status, subtotal,tax_amt,shipping_charge,total_amt,customer_no,employee_no,branch_no)
        values (v_order_no,v_order_date,v_ship_date,v_shipping_method,v_tax_status,v_subtotal,v_tax_amt,v_shipping_charge,v_total_amt,v_customer_no,v_employee_no,v_branch_no);
    end loop;
close c1;
end;
/

执行过程 p_update_orders_grossSales 时出现以下错误:

从第 62 行开始的错误命令 - BEGIN p_update_orders_grossSales; 结尾; 错误报告 - ORA-01422:精确获取返回的行数超过请求的行数 ORA-06512:在“OES2.T_UPDATE_ORDERS_GROSS”,第 5 行 ORA-04088:执行触发器“OES2.T_UPDATE_ORDERS_GROSS”时出错 ORA-06512:在“OES2” .P_UPDATE_ORDERS_GROSSSALES”,第 22 行 ORA-06512:在第 1 行 01422。00000 -“精确提取返回的行数多于请求的行数” *原因:精确提取中指定的数量小于返回的行数。*行动:重写查询或更改请求的行数

为什么会产生错误,如果我一次插入一行,触发器正在工作,但我想用一个过程插入许多记录,并且应该触发触发器以在插入行之前计算插入到 order 表中的行的小计订单更新表。

标签: plsqltriggers

解决方案


当“select into”返回多于 1 行时,Oracle 会引发错误 ORA-01422。在这种情况下,从订单表中选择将返回订单表中的每一行,因为没有 WHERE。您需要添加与更新订单语句相同的 where 子句。您可能需要为“未找到数据异常”做准备。

保护者


推荐阅读