sql - 尝试更新 AFTER UPDATE 触发器上的只读列错误

标签 sql triggers firebird firebird2.5

我正在尝试在 firebird 2.5 上创建一个触发器,以便在更新另一列时更新一列。我为我的尝试创建了一个简单的示例表。

create tablea (estado char(1), fl_previa_laudo char(1));

我的触发器是这样的:

create trigger ATUALIZA_PREVIA_AI for TABLEA
active after update position 0
as
begin

if( old.estado in ('3', '4', '7', '8') ) then
    new.fl_previa_laudo = 'T';
else 
    new.fl_previa_laudo = 'F';
end;

当我运行触发器时,它给出了一个错误:

can't format message 13:849 -- message file C:\Windows\firebird.msg not found. attempted update of read-only column.

最佳答案

您正在尝试修改 AFTER UPDATE 触发器中的列,但这是不可能的。如果要修改值,则需要使用 BEFORE UPDATE 触发器。参见Firebird documentation on triggers ,具体来说(强调我的):

The NEW and OLD variables are subject to some rules:

  • In all triggers, the OLD value is read-only
  • In BEFORE UPDATE and BEFORE INSERT code, the NEW value is read/write, unless it is a COMPUTED BY column
  • In INSERT triggers, references to the OLD variables are invalid and will throw an exception
  • In DELETE triggers, references to the NEW variables are invalid and will throw an exception
  • In all AFTER trigger code, the NEW variables are read-only

换句话说,before update 触发器允许您在持久化行之前修改值,而 after update 触发器在行持久化后触发,从而允许您可以看到最终的值。

关于sql - 尝试更新 AFTER UPDATE 触发器上的只读列错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47905203/

相关文章:

SQL 查询 where 子句太长

mysql - Fun with MySQL - 如何使用 IN 编写删除语句

WPF 样式触发器

mysql - 如果 currentdatetime > datetime+5 则触发删除行

sql - firebird数据库如何显示和设置用户权限

MySQL 存储过程变量

sql - 具有动态偏移量的 SQL 复制 LAG() 函数

postgresql - Postgres 触发器语法

java - 为什么我在使用 jooq 的 Firebird 中出现列未知异常?

sql - Firebird sql更新命令在delphi中不起作用