sql—在oracle触发器中保存另一个表的值

wvmv3b1j  于 2021-08-09  发布在  Java
关注(0)|答案(3)|浏览(431)

我正在写一个触发器,我正在用一堵墙压碎它,因为我需要将另一个表中的值保存在一个变量中,然后将该值减到另一个表的一列中,我正在使用一个触发器,但它不起作用:(
这是我的密码:

create or replace trigger tg_update_total_new_product
after insert on detalle_comanda 
for each row
declare
    var_precio numeric;
begin
    var_precio := (select precio from producto where id=:new.producto_id);
     update comanda set precuenta=precuenta+var_precio where id=:new.comanda_id;
END;

错误代码如下:

Trigger TG_UPDATE_TOTAL_NEW_PRODUCT compiled

LINE/COL  ERROR
--------- -------------------------------------------------------------
4/20      PLS-00103: Encountered the symbol "SELECT" when expecting one of the following:     ( - + case mod new not null <an identifier>    <a double-quoted delimited-identifier> <a bind variable>    continue avg count current exists max min prior sql stddev    sum variance execute forall merge time timestamp interval    date <a string literal with character set specification>    <a number> <a single-quoted SQL string> pipe    <an alternatively-quoted string literal with character set specification>    <an alternat
4/73      PLS-00103: Encountered the symbol ")" when expecting one of the following:     . ( * @ % & - + ; / at for mod remainder rem    <an exponent (**)> and or group having intersect minus order    start union where connect || indicator multiset 
6/4       PLS-00103: Encountered the symbol "end-of-file" when expecting one of the following:     end not pragma final instantiable persistable order    overriding static member constructor map 
Errors: check compiler log

那张表是中间的,然后我在另一张表中有销售总额,我想,当有人添加一个产品时,总额被添加。。。我一直在想一个函数,或者一个视图,但是我不知道为什么这不起作用。。。请帮帮我!谢谢!

7dl7o3gd

7dl7o3gd1#

您应该使用选择。。。进入…:

select precio into var_precio from producto where id=:new.producto_id;
wtzytmuj

wtzytmuj2#

我建议不要使用任何变量,因为您将需要处理未找到的数据、多行获取的异常。您可以直接使用 SELECT 内部声明 UPDATE 声明如下:

create or replace trigger tg_update_total_new_product
after insert on detalle_comanda 
for each row
begin
     update command 
        set precuenta=precuenta 
                      + COALESCE((select precio from producto where id=:new.producto_id),0) 
      where id=:new.comanda_id;
END;
/
628mspwn

628mspwn3#

你犯了个错误- var_precio := (select precio from producto where id=:new.producto_id); 必须使用select[column]into[variable]
因此,正确的语法应该是-

select precio into var_precio from producto where id=:new.producto_id;

相关问题