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

问题描述 投票:0回答:3

我正在写一个触发器,但我被一堵墙压住了,因为我需要把另一个表的值保存在一个变量中,然后把这个值减到另一个表的一个列中,我正在用触发器工作,但它不起作用 :(

这是我的代码。

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

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

sql database oracle
3个回答
1
投票

你应该使用SELECT ... INTO ......。

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

1
投票

我建议不要使用任何变量,因为你需要处理没有找到数据,超过一行的异常。你可以直接使用 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;
/

0
投票

这就是你犯的错误 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;
© www.soinside.com 2019 - 2024. All rights reserved.