Firebird 3.0 数据库中有主表“factgad”和详细表“recgad”。
factgad : factgad_k(pr_k)...
recgad : recgad_k(pr_k), factgad_k(fk)...
当我更新主表时,我必须获取明细表记录的总和,但我无法编写正确的代码。当我尝试更新主表时,出现错误:
Error Message:
----------------------------------------
Too many concurrent executions of the same request.
Too many concurrent executions of the same request.
At trigger 'FACTGAD_AU' line: 21, col: 3
At trigger 'FACTGAD_AU' line: 21, col: 3
At trigger 'FACTGAD_AU' line: 21, col: 3
At trigger 'FACTGAD_AU' line: 21, col: 3
At trigger 'FACTGAD_AU' line: 21, col: 3
At trigger 'FACTGAD_AU' line: 21, col: 3
At trigger 'FACTGAD_AU' line: 21, col: 3
At trigger 'FACTGAD_AU' line: 21, col: 3
At trigger 'FACTGAD_AU' line: 21, col: 3
At trigger 'FACTGAD_AU' line: 21, col: 3
At trigger 'FACTGAD_AU' line: 21, col: 3
At trigger 'FACTGAD_AU' line: 21, col: 3
At trigger 'FACTGAD_AU' line: 21, col: 3
At trigger 'FACTGAD_AU' line: 21, col: 3
At trigger 'FACTGAD_AU' line: 21, col: 3
At trigger 'FACTGAD_AU' line: 21, col: 3
At trigger 'FACTGAD_AU' line: 21, col: 3
At trigger 'FACTGAD_AU' line: 21, col: 3
At trigger 'FACTGAD_AU' line: 21, col: 3
At trigger 'FACTGAD_AU' line: 21, col: 3
At trigger 'FACTGAD_AU' line: 21, col: 3
At trigger 'FACTGAD_AU' line: 21, col: 3
At trigger 'FACTGAD_AU' line: 21, col: 3
At trigger 'FACTGAD_AU' line: 21, col: 3
At trigger 'FACTGAD_AU' line: 21, co...
---------------------------------------------------
SQLCODE: -693
SQLSTATE: 54001
GDSCODE: 335544663
create or alter trigger factgad_AU FOR factgad
ACTIVE AFTER UPDATE POSITION 0
AS
DECLARE VARIABLE newl DECIMAL(8, 4);
DECLARE VARIABLE oldl DECIMAL(8, 4);
DECLARE VARIABLE newd DECIMAL(8, 4);
DECLARE VARIABLE oldd DECIMAL(8, 4);
DECLARE VARIABLE FACTGAD_K INTEGER;
BEGIN
select
f.factgad_k,
sum(r.fnewl*r.rgad),
sum(r.foldl*r.rgad),
sum(r.fnewd*r.rgad),
sum(r.foldd*r.rgad)
from recgad r, factgad f
where f.factgad_k=new.factgad_k and r.factgad_k=f.factgad_k
group by f.factgad_k
into :factgad_k,:NEWL, :OLDL, :NEWD, :OLDD;
update factgad set
factgad.NEWL=:NEWL,
factgad.OLDL=:OLDL,
factgad.NEWD=:NEWD,
factgad.OLDD=:OLDD
where factgad_k=:FACTGAD_K;
end
触发 SQL 有什么问题?我尝试更改 where 子句where f.factgad_k=new.factgad_k
,where f.factgad_k=43
但出现相同的错误。重新启动 Firebird 服务没有任何改变。
奇怪的行为,更新触发器后出现相同的错误,它在更新详细表后用详细表的总和更新主表:
CREATE OR ALTER TRIGGER RECGAD_AU FOR RECGAD
ACTIVE AFTER UPDATE POSITION 0
AS
DECLARE VARIABLE NEWL DECIMAL(8, 4);
DECLARE VARIABLE OLDL DECIMAL(8, 4);
DECLARE VARIABLE NEWD DECIMAL(8, 4);
DECLARE VARIABLE OLDD DECIMAL(8, 4);
DECLARE VARIABLE FACTGAD_K INTEGER;
begin
select
r.factgad_k,
sum(r.fnewl*r.rgad),
sum(r.foldl*r.rgad),
sum(r.fnewd*r.rgad),
sum(r.foldd*r.rgad)
from recgad r, factgad f
where r.factgad_k=new.factgad_k and r.factgad_k=f.factgad_k
group by r.factgad_k
into :factgad_k,:NEWL, :OLDL, :NEWD, :OLDD;
update factgad set
factgad.NEWL=:NEWL,
factgad.OLDL=:OLDL,
factgad.NEWD=:NEWD,
factgad.OLDD=:OLDD
where factgad_k=:FACTGAD_K;
end