在触发器主体中指定触发器的父架构

发布于 2024-12-20 02:16:43 字数 910 浏览 4 评论 0原文

DB2 for IBM System i 中,我创建了此触发器,用于记录 MYLOGTABLE 上对 MYCHECKEDTABLE 进行的每个插入操作:

SET SCHEMA MYSCHEMA;

CREATE TRIGGER MYTRIGGER AFTER INSERT ON MYCHECKEDTABLE
REFERENCING NEW AS ROWREF
FOR EACH ROW BEGIN ATOMIC
     INSERT INTO MYLOGTABLE -- after creation becomes MYSCHEMA.MYLOGTABLE
         (MMACOD, OPTYPE, OPDATE)
     VALUES (ROWREF.ID, 'I', CURRENT TIMESTAMP);
END;

DBMS 将触发器主体存储为 < code>MYSCHEMA.MYLOGTABLE 硬编码。

现在假设我们将整个模式复制为新模式NEWSCHEMA。当我在 NEWSCHEMA.MYCHECKEDTABLE 中插入记录时,日志记录将被添加到 MYSCHEMA.MYLOGTABLE 而不是 NEWSCHEMA.MYLOGTABLE,即在架构中触发器及其表所在的位置。这是造成大问题的原因!!另外,因为许多用户可以在没有我控制的情况下复制架构...

那么,有没有办法在触发器主体中指定触发器所在的架构?通过这种方式,我们将编写正确的 MYLOGTABLE 中的日志记录。像PARENT SCHEMA之类的东西...或者有解决方法吗? 非常感谢!

In DB2 for IBM System i I create this trigger for recording on MYLOGTABLE every insert operation made on MYCHECKEDTABLE:

SET SCHEMA MYSCHEMA;

CREATE TRIGGER MYTRIGGER AFTER INSERT ON MYCHECKEDTABLE
REFERENCING NEW AS ROWREF
FOR EACH ROW BEGIN ATOMIC
     INSERT INTO MYLOGTABLE -- after creation becomes MYSCHEMA.MYLOGTABLE
         (MMACOD, OPTYPE, OPDATE)
     VALUES (ROWREF.ID, 'I', CURRENT TIMESTAMP);
END;

The DBMS stores the trigger body with MYSCHEMA.MYLOGTABLE hardcoded.

Now imagine that we copy the entire schema as a new schema NEWSCHEMA. When I insert a record in NEWSCHEMA.MYCHECKEDTABLE a log record will be added to MYSCHEMA.MYLOGTABLE instead of NEWSCHEMA.MYLOGTABLE, i.e. in the schema where trigger and its table live. This is cause of big issues!! Also because many users can copy the schema without my control...

So, is there a way to specify, in the trigger body, the schema where the trigger lives? In this way we'll write the log record in the correct MYLOGTABLE. Something like PARENT SCHEMA... Or is there a workaround?
Many thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只为守护你 2024-12-27 02:16:43

HLL 中定义的外部触发器可以访问触发器缓冲区,其中包括触发触发器的表的库名称。这可用于限定对 MYLOGTABLE 的引用。

请参阅 IBM 红皮书第 11.2 章“触发器程序结构” 存储过程、触发器和用户定义有关更多信息,请访问 DB2 Universal Database for iSeries 上的函数。

或者,您可以使用 CURRENT SCHEMA 特殊寄存器或GET DESCRIPTOR 语句找出触发器和/或表当前所在的位置。

External triggers defined in an HLL have access to a trigger buffer that includes the library name of the table that fired the trigger. This could be used to qualify the reference to the MYLOGTABLE.

See chapter 11.2 "Trigger program structure" of the IBM Redbook Stored Procedures, Triggers, and User-Defined Functions on DB2 Universal Database for iSeries for more information.

Alternatively you may be able to use the CURRENT SCHEMA special register or the GET DESCRIPTOR statement to find out where the trigger and/or table are currently located.

韬韬不绝 2024-12-27 02:16:43

不幸的是,我意识到触发器所在的模式无法从触发器体内检测

但有一些解决方法(感谢 @krmilligan 也):

  • 剥夺用户执行CPYLIB的权限并让他们使用实用程序。
  • 在系统上创建一个后台代理,定期运行以查找不同步的触发器。
  • 对于命令CPYLIB,将TRG选项的默认值设置为*NO。通过这种方式,触发器将永远不会被复制,除非用户明确指定它。

我选择最后一种,因为它是最简单的一种,即使可能存在需要触发器复制的上下文。在这种情况下,我会采取第一种解决方法。

Unfortunately I realized that the schema where a trigger lives can't be detected from inside trigger's body.

But there are some workarounds (thanks to @krmilligan too):

  • Take away the user's authority to execute CPYLIB and make them use a utility.
  • Create a background agent on the system that peridiocally runs looking for triggers that are out of synch.
  • For command CPYLIB set the default for TRG option to *NO. In this way triggers will never be copied, except if the user explicitly specifies it.

I choose the last one because it's the simplest one, even if there can be contexts where trigger copy is required. In such cases I'd take the first workaround.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文