1

我有一个来自insert, update, delete,的触发器

在此更新中,每次运行时都会超出堆栈深度限制update并进入无限循环。(无限递归)

有人有任何提示吗?

CREATE OR REPLACE FUNCTION saldolinear() RETURNS TRIGGER AS 
'
DECLARE
    IDS  INTEGER:= 0;   MIN integer:= 0; MIN1 integer:= 0; valor1 float;saldo float;    saldoini float; saldoini1 float;i integer:=0;j integer:= 0;
    l integer:=0;   m integer:=0;var2 integer:=0;countid integer:=0;coun integer:=0;DATADB date;IDMAX integer:=0;dataup date;
    coundata date;  res boolean;    res1 boolean;   mov TB_MOV_BANCARIA%rowtype; registro integer;
BEGIN
     IF (TG_OP = ''INSERT'' ) THEN
        SELECT INTO registro count(TB_MOV_BANCARIA.data) FROM TB_MOV_BANCARIA where TB_MOV_BANCARIA.data<NEW.data group by TB_MOV_BANCARIA.data;
    saldoini:=0;        
    IF (registro>0) THEN    
       res:= true;
       SELECT INTO DATADB MAX(TB_MOV_BANCARIA.data) FROM TB_MOV_BANCARIA where TB_MOV_BANCARIA.data<NEW.data;
       FOR mov IN select tb_mov_bancaria.id from tb_mov_bancaria where tb_mov_bancaria.data = DATADB order by tb_mov_bancaria.data, tb_mov_bancaria.id LOOP
          SELECT INTO saldoini saldo_linha FROM TB_MOV_BANCARIA where id = mov.id;
       END LOOP;
    END IF;       
           FOR mov IN select * from tb_mov_bancaria where tb_mov_bancaria.data>=NEW.data order by tb_mov_bancaria.data, tb_mov_bancaria.id LOOP
              saldoini := saldoini + mov.valor;
          UPDATE tb_mov_bancaria set saldo_linha=saldoini where id = mov.id;
       END LOOP;
     END IF;
     IF (TG_OP = ''UPDATE'' ) THEN
        IF(OLD.data<NEW.data)THEN
       dataup:= OLD.data;
    ELSE
       dataup:= NEW.data;
        END IF;
        IF (NOT dataup is null) THEN
           SELECT INTO DATADB MAX(TB_MOV_BANCARIA.data) FROM TB_MOV_BANCARIA where TB_MOV_BANCARIA.data<dataup;
       FOR mov IN select tb_mov_bancaria.id from tb_mov_bancaria where tb_mov_bancaria.data = DATADB order by tb_mov_bancaria.data, tb_mov_bancaria.id LOOP
          SELECT INTO saldoini saldo_linha FROM TB_MOV_BANCARIA where id = mov.id;
       END LOOP;
       --RAISE EXCEPTION ''%'',dataup;
       FOR mov IN select * from tb_mov_bancaria where tb_mov_bancaria.data>=dataup order by tb_mov_bancaria.data, tb_mov_bancaria.id LOOP
              saldoini := saldoini + mov.valor;
              --RAISE EXCEPTION ''UPDATE tb_mov_bancaria set saldo_linha=% where id = %;'',saldoini,mov.id;
          UPDATE tb_mov_bancaria set saldo_linha=saldoini where id = mov.id;
       END LOOP;
      END IF;
     END IF;
      IF (TG_OP = ''DELETE'') THEN
       res:= true;
       SELECT INTO DATADB MAX(TB_MOV_BANCARIA.data) FROM TB_MOV_BANCARIA where TB_MOV_BANCARIA.data<OLD.data;
       FOR mov IN select tb_mov_bancaria.id from tb_mov_bancaria where  tb_mov_bancaria.data = DATADB order by tb_mov_bancaria.data, tb_mov_bancaria.id LOOP
          SELECT INTO saldoini saldo_linha FROM TB_MOV_BANCARIA where id = mov.id;
       END LOOP;

           FOR mov IN select * from tb_mov_bancaria where tb_mov_bancaria.data>=OLD.data           order by tb_mov_bancaria.data, tb_mov_bancaria.id LOOP
                saldoini := saldoini + mov.valor;
            UPDATE tb_mov_bancaria set saldo_linha=saldoini where id = mov.id;
         END LOOP;
       END IF;
           RETURN NULL;
   END
  '
  LANGUAGE 'plpgsql'
4

1 回答 1

1

我认为你的触发器做错了。触发器几乎不应该递归,如果它递归,你必须确保它不能无限递归。

无限递归发生是因为您尝试再次执行操作,而不是仅仅修改操作发生的内容。您返回 null,这表明您已经完成了您打算做的所有事情。

无需重做更新语句等,只需返回一个新值,包括修改后的数据。仅使用 UPDATE 或 DELETE 来修改原始请求未修改的表(或者如果您绝对必须修改行)。

于 2013-11-28T09:08:47.217 回答