根据触发器中的动态column_name动态column_value

发布于 2024-12-23 01:12:41 字数 1887 浏览 2 评论 0原文

我需要执行一个触发器,但我想做的唯一区别是根据 ':new.COLUMN_NAME' 和 ':old.COLUMN_NAME' 获取动态值

我在查询中引入表的列之后我用 for LOOP 运行它..

这是我的触发器:

CREATE OR REPLACE TRIGGER aft_ins_soliccambio
AFTER INSERT OR DELETE OR UPDATE
ON SEG_V_SOLICCAMBIO
REFERENCING OLD AS OLD NEW AS NEW
FOR EACH ROW
DECLARE
   old_col_value   VARCHAR2 (4000);
   new_col_value   VARCHAR2 (4000);
BEGIN
   FOR REC IN (  SELECT OWNER, TABLE_NAME, COLUMN_NAME
                   FROM all_tab_columns
                  WHERE OWNER = 'EUCEDA' AND (TABLE_NAME = 'SEG_V_SOLICCAMBIO')
               ORDER BY column_id)
   LOOP
      EXECUTE IMMEDIATE ' select :new.' || REC.COLUMN_NAME || ' from dual'
               INTO new_col_value;

      pkg_tumi.insert_auditoria ('SEG_V_SOLICCAMBIO',
                                 REC.COLUMN_NAME,
                                 :new.EMPR_IDEMPRESA_N,
                                 fn_get_pk ('SEG_V_SOLICCAMBIO', 1),
                                 fn_get_pk ('SEG_V_SOLICCAMBIO', 2),
                                 fn_get_pk ('SEG_V_SOLICCAMBIO', 3),
                                 fn_get_pk ('SEG_V_SOLICCAMBIO', 4),
                                 fn_get_pk ('SEG_V_SOLICCAMBIO', 5),
                                 fn_get_pk ('SEG_V_SOLICCAMBIO', 6),
                                 1,
                                 '',
                                 new_col_value,
                                 SYSDATE,
                                 NULL);
   END LOOP;
END;

当我编译触发器时,它不会抛出任何错误,但是当我尝试在我正在创建触发器的表中插入新值时,它会抛出这个错误:

ORA-01008: 没有任何 las 变量 han sido enlazadas ORA-06512: zh "EUCEDA.AFT_INS_SOLICCAMBIO",第 10 行 ORA-04088: 执行错误期间出现错误 'EUCEDA.AFT_INS_SOLICCAMBIO'

检查问题时我意识到问题出在 'EXECUTE IMMEDIATE ..' 之间,但我发现这是因为 :new.'dynamic_column' 无法识别。请帮忙!我花了很多时间解决这个问题,但无法解决。

谢谢并抱歉我的英语。

I need to do a trigger but the only difference that I'm trying to do is to get a dynamic value according to ':new.COLUMN_NAME' and ':old.COLUMN_NAME'

I'm bringing the columns of a table in a query and after that I run it with a for LOOP ..

this is my trigger:

CREATE OR REPLACE TRIGGER aft_ins_soliccambio
AFTER INSERT OR DELETE OR UPDATE
ON SEG_V_SOLICCAMBIO
REFERENCING OLD AS OLD NEW AS NEW
FOR EACH ROW
DECLARE
   old_col_value   VARCHAR2 (4000);
   new_col_value   VARCHAR2 (4000);
BEGIN
   FOR REC IN (  SELECT OWNER, TABLE_NAME, COLUMN_NAME
                   FROM all_tab_columns
                  WHERE OWNER = 'EUCEDA' AND (TABLE_NAME = 'SEG_V_SOLICCAMBIO')
               ORDER BY column_id)
   LOOP
      EXECUTE IMMEDIATE ' select :new.' || REC.COLUMN_NAME || ' from dual'
               INTO new_col_value;

      pkg_tumi.insert_auditoria ('SEG_V_SOLICCAMBIO',
                                 REC.COLUMN_NAME,
                                 :new.EMPR_IDEMPRESA_N,
                                 fn_get_pk ('SEG_V_SOLICCAMBIO', 1),
                                 fn_get_pk ('SEG_V_SOLICCAMBIO', 2),
                                 fn_get_pk ('SEG_V_SOLICCAMBIO', 3),
                                 fn_get_pk ('SEG_V_SOLICCAMBIO', 4),
                                 fn_get_pk ('SEG_V_SOLICCAMBIO', 5),
                                 fn_get_pk ('SEG_V_SOLICCAMBIO', 6),
                                 1,
                                 '',
                                 new_col_value,
                                 SYSDATE,
                                 NULL);
   END LOOP;
END;

when I compile the trigger it doesn't throw any error but when I try inserting a new value in the table that I'm making the trigger it throws this error:

ORA-01008: no todas las variables han sido enlazadas
ORA-06512: en "EUCEDA.AFT_INS_SOLICCAMBIO", línea 10
ORA-04088: error durante la ejecución del disparador 'EUCEDA.AFT_INS_SOLICCAMBIO'

Checking the problem I realized that the problem is between 'EXECUTE IMMEDIATE ..' but I see that is because of :new.'dynamic_column' is not recognized. Please Help! I'm many hours with this problem and I can't solve it.

Thanks and sorry for my english.

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

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

发布评论

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

评论(1

深府石板幽径 2024-12-30 01:12:41

触发器内的 :new:old 有点不灵活。我不知道有什么方法可以使用动态 SQL 来执行您尝试使用 EXECUTE IMMEDIATE 执行的操作。我怀疑这是不可能的。

恐怕我所知道的唯一替代方案有些费力。其中一种替代方法是用 CASE 表达式替换 EXECUTE IMMEDIATE,如下所示:

    new_col_value := CASE REC.COLUMN_NAME
                       WHEN 'COLUMN_1' THEN :new.column_1
                       WHEN 'COLUMN_2' THEN :new.column_2
                       WHEN 'COLUMN_3' THEN :new.column_3
                       ...
                     END;

可以运行合适的 SQL 语句来生成所有 WHEN ... THEN ... 行在此 CASE 语句中,而不是您自己将它们全部键入。

:new and :old within triggers are somewhat inflexible. I don't know of any way of using dynamic SQL to do what you're attempting to do with your EXECUTE IMMEDIATE. I suspect it's not possible.

I'm afraid the only alternatives I know about are somewhat laborious. One such alternative is to replace the EXECUTE IMMEDIATE with a CASE expression such as the following:

    new_col_value := CASE REC.COLUMN_NAME
                       WHEN 'COLUMN_1' THEN :new.column_1
                       WHEN 'COLUMN_2' THEN :new.column_2
                       WHEN 'COLUMN_3' THEN :new.column_3
                       ...
                     END;

It might be possible to run a suitable SQL statement to generate all of the WHEN ... THEN ... lines within this CASE statement rather than typing them all in yourself.

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