根据触发器中的动态column_name动态column_value
我需要执行一个触发器,但我想做的唯一区别是根据 ':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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
触发器内的
:new
和:old
有点不灵活。我不知道有什么方法可以使用动态 SQL 来执行您尝试使用EXECUTE IMMEDIATE
执行的操作。我怀疑这是不可能的。恐怕我所知道的唯一替代方案有些费力。其中一种替代方法是用
CASE
表达式替换EXECUTE IMMEDIATE
,如下所示:可以运行合适的 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 yourEXECUTE 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 aCASE
expression such as the following:It might be possible to run a suitable SQL statement to generate all of the
WHEN ... THEN ...
lines within thisCASE
statement rather than typing them all in yourself.