我有来自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'
答案 0 :(得分:1)
我认为你的触发器做错了。一个触发器应该几乎从不递归,如果它是递归的,你必须确保它无法无限地递归。
无限递归发生是因为您尝试再次执行操作而不是仅修改操作发生的操作。你返回null,表示你已经完成了你想做的一切。
不是重做更新语句等,而是返回包含修改数据的NEW值。仅使用UPDATE或DELETE来修改未被原始请求修改的表(或者如果绝对必须,则行)。