0
Hello, I have a table where I keep records of movements, it is inserted a record where it is possible to inform a payment installment, for example, a purchase made in 12 times whose current installment (which was manually inserted in the bank) is the installment 8/12, missing still 4 installments, so when entering this record Trigger must enter the records of installments 9, 10, 11 and 12, all other data must be the same, being only the number of the different installment, follows the code I did:
CREATE FUNCTION fn_parcelas_futuras()
RETURNS TRIGGER
LANGUAGE PLPGSQL
AS $$
BEGIN
IF NEW.quantidade_parcelas > 1 THEN
FOR mes_parcela IN NEW.parcela_atual..NEW.quantidade_parcelas LOOP
INSERT INTO movimentacoes(
operacao,
nome_operacao,
id_estabelecimento,
id_cartao,
parcela_atual,
quantidade_parcelas,
valor_parcela,
mes_fatura
)
VALUES(
NEW.operacao,
NEW.nome_operacao,
NEW.id_estabelecimento,
NEW.id_cartao,
mes_parcela,
NEW.quantidade_parcelas,
NEW.valor_parcela,
NEW.mes_fatura
);
END LOOP;
END IF;
RETURN NEW;
END;
$$
CREATE TRIGGER tg_inserir_parcelas_futuras
AFTER INSERT
ON movimentacoes
FOR EACH ROW
EXECUTE PROCEDURE fn_parcelas_futuras();
When I try to enter some data I get the following error:
ERROR: stack depth limit exceeded
HINT: Increase the configuration parameter "max_stack_depth" (currently 2048kB), after ensuring the platform's stack depth limit is adequate.
CONTEXT: SQL statement "SELECT 1 FROM ONLY "public"."cartoes" x WHERE "codigo" OPERATOR(pg_catalog.=) $1 FOR KEY SHARE OF x"
SQL statement "INSERT INTO movimentacoes(
operacao,
nome_operacao,
id_estabelecimento,
id_cartao,
parcela_atual,
quantidade_parcelas,
valor_parcela,
mes_fatura
)
VALUES(
NEW.operacao,
NEW.nome_operacao,
NEW.id_estabelecimento,
NEW.id_cartao,
mes_parcela,
NEW.quantidade_parcelas,
NEW.valor_parcela,
NEW.mes_fatura
)"
PL/pgSQL function fn_parcelas_futuras() line 5 at SQL statement
SQL statement "INSERT INTO movimentacoes(
operacao,
nome_operacao,
id_estabelecimento,
id_cartao,
parcela_atual,
quantidade_parcelas,
valor_parcela,
mes_fatura
)
VALUES(
NEW.operacao,
NEW.nome_operacao,
NEW.id_estabelecimento,
NEW.id_cartao,
mes_parcela,
NEW.quantidade_parcelas,
NEW.valor_parcela,
NEW.mes_fatura
)"
[...]
REPETE ULTIMO BLOCO ALGUMAS VEZES.
[...]
PL/pgSQL function fn_parcelas_futuras() line 5 at SQL statement
SQL state: 54001
"Trigger must enter the records of plots 9, 10, 11 and 12" Trigger doesn’t quite have that purpose, you need to make a
procedure
which is the most correct object to do this kind of operation– Ricardo Pontual
Right, in case the
trigger
should call theprocedure
so that’s it?– bruno101
The Trigger gets recursive (stack Depth limit exceeded) but this logic is not making sense , what entered the "8" ?! A file must be entered in tranche 1 by "n".
– Motta
@bruno101 would be better for all the code in the Trial without Rigger, does all the Inserts in "drives" in a single process, clear and well defined, even can be transactional, commit when generating all the plots, does not need a Trigger
– Ricardo Pontual
@Motta I mentioned there, the parcel 8 would have been inserted manually, then the bank itself should insert the remaining installments, if it had been inserted the parcel 1/12 all the others should be inserted, if it were the 12/12 nothing would be done, because it is already the last installment.
– bruno101
@Ricardopunctual but the
procedure
would not need atrigger
to be called? Even if the whole process is done in theprocedure
.– bruno101
The problem is the Rigger is recursive , will insert for example from 8 to 12 , when inserting the 8 the Rigger inserts in the table and fires from 9 to 12 and so on , the Rigger does not seem to fit here in this case.
– Motta
@bruno101 "the project would not need a Rigger to be called" of course not, if it were so, for the whole trial would need a Rigger :) instead of running a
insert
executes the precedent that will make all the Inserts at once, will change only that in the way to make the call– Ricardo Pontual
@Motta understood what you meant, in fact, from the call 9 to 12 we would have
triggers
being fired accordingly at each newinsert
, in fact this is not the desired behavior.– bruno101
@Ricardopunctual in this case I would make a
procedure
to enter data, this would be responsible for assessing the situation and need for entering the other data. Would that be? Something like:pr_insert_movimentacoes(<parâmetros do insert aqui>)
– bruno101
that, then you can control everything in the same process
– Ricardo Pontual