触发更新包含信息的行

时间:2016-09-16 02:41:20

标签: mysql stored-procedures triggers plsqldeveloper

大家好,我试图触发更新表中的信息,但此表有信息,此信息来自此程序存储

create or replace procedure insertar (new_bod int, new_arti int, cant int)
BEGIN
  INSERT INTO bod_arti (bod_id, arti_id, cant)
       VALUES (new_bod , new_arti, new_cant);
END

在这个过程中,bod_id是来自BOD表的PK,arti_id是来自ARTI表的PK,当我尝试插入调用过程的值时,我有一个错误,这是因为PK是相同的。 我怎么能只在表中插入并添加信息?我有这个触发器

create trigger myfirsttrigger BEFORE INSERT ON bod_arti
FOR EACH ROW
UPDATE bod_arti SET cant = cant + NEW.cant WHERE bod_id = NEW.bod_id AND arti_id = NEW.arti_id

当我再次使用我已插入的值调用该过程时出现此错误

1442 - 无法更新表格' bod_arti'在存储的函数/触发器中,因为它已被调用此存储函数/触发器的语句使用。

我该怎么办? 谢谢!

1 个答案:

答案 0 :(得分:0)

在oracle PLSQL中,了解如何实现以下目标:

CREATE OR REPLACE PROCEDURE insertar (new_bod    INT,
                                      new_arti   INT,
                                      new_cant   INT)
AS
BEGIN
   INSERT INTO bod_arti (bod_id, arti_id, cant)
        VALUES (new_bod, new_arti, new_cant);

   COMMIT;
END;

触发器:

CREATE OR REPLACE TRIGGER myfirsttrigger
   BEFORE INSERT
   ON bod_arti
   FOR EACH ROW
BEGIN

/*In place of writing a update you can try as below*/
--   UPDATE bod_arti
--      SET cant = cant + :NEW.cant
--    WHERE bod_id = :NEW.bod_id AND arti_id = :NEW.arti_id;

     ---Create autonomous transaction
   proc_upd(:old.bod_id ,:old.arti_id,:old.cant); 

END;

自主交易

create or replace  procedure proc_upd(num1 number,num2 number, num3 number)
as
PRAGMA AUTONOMOUS_TRANSACTION;
begin

   UPDATE bod_arti     
      SET cant = cant + num3       
   WHERE bod_id = num1
    AND arti_id = num2;

    commit;

END; 

多次通话:

exec insertar(1,2,3);
exec insertar(4,5,6);

<强> 输出:

SQL> select * from bod_arti;

    BOD_ID    ARTI_ID       CANT
---------- ---------- ----------
         1          2          6
         4          5          12