我现在正在为 Oracle 中的 DELETE 触发器进行长期斗争,该触发器在删除一行后,会从剩余的行中选择一个新的 MAX 值并将其写入另一个表。在偶然发现烦人的 ORA-04091 变异表错误(在 FOR EACH ROW 时无法读取表)后,我切换到 Oracle 的复合触发器。
我怎样才能最好地存储已删除的行(每行有多个值,因为只有在删除的分数可能是高分而不是低分时,进一步检查才会更新)?我担心如果多个触发事件交叉触发并且例如为“DeletedMatches”运行高分更新,该全局临时表可能会以一团糟而告终,这些“DeletedMatches”实际上并没有被删除,而是由触发事件之前注册。
我可以创建一个表,它 a) 仅在此触发器中本地存在 b) 可以像普通数据库表或临时表一样在 SQL 中使用?
每当匹配被删除时,以下(伪)代码将更新 CurrentHighScores 表(旧的高分消失并由最高的剩余分数代替)。
CREATE TABLE GameScores (
MatchId number not null --primary key
Player varchar(255) not null,
Game varchar(255) not null, -- PacMan, Pong, whatever...
Score number not null );
-- High score for each game:
CREATE TABLE CurrentHighScores (
HiScId number not null --primary key
Player varchar(255) not null,
Game varchar(255) not null,
HighScore number not null );
create or replace TRIGGER UpdHiScoreOnMatchDelete
FOR DELETE ON GameScores
COMPOUND TRIGGER
TYPE matchtable IS TABLE OF GameScores%ROWTYPE INDEX BY SIMPLE_INTEGER;
DeletedMatches matchtable;
MatchIndex SIMPLE_INTEGER := 0;
BEFORE EACH ROW IS -- collect deleted match scores
BEGIN
MatchIndex:= MatchIndex+ 1;
DeletedMatches(MatchIndex).Game := :old.Game;
DeletedMatches(MatchIndex).Score := :old.Score;
-- don't want to set every column value, want to
-- do like: INSERT :old INTO DeletedMatches;
-- don't want the Index either!
END BEFORE EACH ROW;
AFTER STATEMENT IS
BEGIN
UPDATE CurrentHighScores hsc
SET hsc.HighScore=(
select max(gsc.Score) from GameScores gsc
where hsc.Game=gsc.Game)
where hsc.Game IN (
select del.Game from DeletedMatches del where hsc.HighScore = del.Score)
-- won't work, how can I check within the SQL if a row
-- for this game has been deleted, or anyhow integrate
-- DeletedMatches into the SQL, without a cursor?
-- Optional further cond. in subselect, to update only
-- if deleted score equals highscore:
and exists(
select 1 from GameScores where Game=hsc.Game);
-- ignore games without remaining match scores.
-- Delete/set zero code for games without existing scores omitted here.
END AFTER STATEMENT;