DB2触发器在更新后,更新同一表中已更新行的单个列。

huangapple go评论46阅读模式
英文:

DB2 trigger after update, update singl;e column n the updated row in the same table

问题

使用云上的DB2(LUW)
我想在行中的任何其他值更新后,更新行中的单个列值。

以下AFTER INSERT触发器有效:

CREATE TRIGGER CENTRAL_REPOSITORY.TR_DATABASE_OBJECT_INS_CDT
 AFTER INSERT ON CENTRAL_REPOSITORY.DATABASE_OBJECT
 REFERENCING NEW AS N_ROW
 FOR EACH ROW
 UPDATE CENTRAL_REPOSITORY.DATABASE_OBJECT SET create_datetime = current_timestamp
 	where id = N_ROW.id

以下AFTER UPDATE触发器无效:

CREATE TRIGGER CENTRAL_REPOSITORY.TR_DATABASE_OBJECT_UPD_UDT
 AFTER UPDATE ON CENTRAL_REPOSITORY.DATABASE_OBJECT
 REFERENCING NEW AS N_ROW
 FOR EACH ROW
 UPDATE CENTRAL_REPOSITORY.DATABASE_OBJECT SET update_datetime = current_timestamp
 	where id = N_ROW.id;

尝试在表上运行插入操作后,出现以下错误:

"在触发器“CENTRAL_REPOSITO”中触发的SQL语句发生错误。错误返回的信息包括SQLCODE“-724”,SQLSTATE“54038”和消息标记“CENTRAL_REPOSITORY.TR_DATABASE_OBJECT_UPD_”。SQLCODE=-723,SQLSTATE=09000,DRIVER=4.32.28"

英文:

Using DB2 on cloud ( LUW )
I want to update a single column value in a row, after any of the other values in that row get updated.

The following AFTER INSERT works

CREATE TRIGGER CENTRAL_REPOSITORY.TR_DATABASE_OBJECT_INS_CDT
 AFTER INSERT ON CENTRAL_REPOSITORY.DATABASE_OBJECT
 REFERENCING NEW AS N_ROW
 FOR EACH ROW
 UPDATE CENTRAL_REPOSITORY.DATABASE_OBJECT SET create_datetime = current_timestamp
 	where id = N_ROW.id

The following AFTER UPDATE does not

CREATE TRIGGER CENTRAL_REPOSITORY.TR_DATABASE_OBJECT_UPD_UDT
 AFTER UPDATE ON CENTRAL_REPOSITORY.DATABASE_OBJECT
 REFERENCING NEW AS N_ROW
 FOR EACH ROW
 UPDATE CENTRAL_REPOSITORY.DATABASE_OBJECT SET update_datetime = current_timestamp
 	where id = N_ROW.id;

The error I get after trying to run an insert on the table is

"An error occurred in a triggered SQL statement in trigger "CENTRAL_REPOSITO". Information returned for the error includes SQLCODE "-724", SQLSTATE "54038" and message tokens "CENTRAL_REPOSITORY.TR_DATABASE_OBJECT_UPD_".. SQLCODE=-723, SQLSTATE=09000, DRIVER=4.32.28"

答案1

得分: 2

以下是翻译好的部分:

  • 不应使用AFTER触发器,
  • 使用BEFORE触发器...
  • 创建触发器CENTRAL_REPOSITORY.TR_DATABASE_OBJECT_UPD_UDT
    在CENTRAL_REPOSITORY.DATABASE_OBJECT上进行UPDATE之前
    引用NEW AS N_ROW
    对于每一行
    设置n_row.update_datetime = current_timestamp;
  • 创建触发器CENTRAL_REPOSITORY.TR_DATABASE_OBJECT_UPD_UDT
    在CENTRAL_REPOSITORY.DATABASE_OBJECT上进行INSERT之前
    引用NEW AS N_ROW
    对于每一行
    设置n_row.update_datetime = current_timestamp;
  • 编辑
  • 您可以考虑将列定义为“行更改时间戳”,然后根本不需要使用触发器。
英文:

You shouldn't be using an AFTER trigger,

Use a BEFORE trigger...

CREATE TRIGGER CENTRAL_REPOSITORY.TR_DATABASE_OBJECT_UPD_UDT
 BEFORE UPDATE ON CENTRAL_REPOSITORY.DATABASE_OBJECT
 REFERENCING NEW AS N_ROW
 FOR EACH ROW
  SET n_row.update_datetime = current_timestamp;

CREATE TRIGGER CENTRAL_REPOSITORY.TR_DATABASE_OBJECT_UPD_UDT
 BEFORE INSERT ON CENTRAL_REPOSITORY.DATABASE_OBJECT
 REFERENCING NEW AS N_ROW
 FOR EACH ROW
  SET n_row.update_datetime = current_timestamp;

EDIT
You might consider defining the column a "row change timestamp", then you won't need to bother with the triggers at all.

答案2

得分: 0

You get this error (SQLCODE = -724) also known as "SQL0724N" when Db2 finds more than one object might get activated if your trigger fires, typically because you have other triggers or RI on the table whose conditions are not mutually exclusive or may cause recursion.

Look at the docs

which includes the following advice:

Explanation

Cascading of indirect SQL occurs when a trigger activates another
trigger (possibly through referential constraint delete rules) or a
routine, containing SQL, invokes another routine. The depth of this
cascading is limited to 16 for triggers and 64 for routines.

Note that recursive situations where a trigger includes a triggered
SQL statement that directly or indirectly causes the same trigger to
be activated, or where a routine directly or indirectly invokes
itself, is a form of cascading that is very likely to cause this error
if there are no conditions to prevent cascading from exceeding the
limit.

The object-type is one of TRIGGER, FUNCTION, METHOD, or PROCEDURE.

The object-name specified is one of the objects that would have been
activated at the seventeenth level of cascading.

User response

Start with the objects that are activated or invoked by the statement
that received this error. If any of these objects are recursive,
ensure that there is some condition that prevents the object from
being activated or invoked more than the limit allows. If this is not
the cause of the problem, follow the chain of objects that are
activated or invoked to determine the chain that exceeds the cascading
limit.

英文:

You get this error (SQLCODE = -724) also known as "SQL0724N" when Db2 finds more than one object might get activated if your trigger fires, typically because you have other triggers or RI on the table whose conditions are not mutually exclusive or may cause recursion.

Look at the docs

which includes the following advice:

> Explanation
>
> Cascading of indirect SQL occurs when a trigger activates another
> trigger (possibly through referential constraint delete rules) or a
> routine, containing SQL, invokes another routine. The depth of this
> cascading is limited to 16 for triggers and 64 for routines.
>
> Note that recursive situations where a trigger includes a triggered
> SQL statement that directly or indirectly causes the same trigger to
> be activated, or where a routine directly or indirectly invokes
> itself, is a form of cascading that is very likely to cause this error
> if there are no conditions to prevent cascading from exceeding the
> limit.
>
> The object-type is one of TRIGGER, FUNCTION, METHOD, or PROCEDURE.
>
> The object-name specified is one of the objects that would have been
> activated at the seventeenth level of cascading.

> User response
>
> Start with the objects that are activated or invoked by the statement
> that received this error. If any of these objects are recursive,
> ensure that there is some condition that prevents the object from
> being activated or invoked more than the limit allows. If this is not
> the cause of the problem, follow the chain of objects that are
> activated or invoked to determine the chain that exceeds the cascading
> limit.

huangapple
  • 本文由 发表于 2023年4月1日 00:34:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75900828.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定