【问题标题】:Oracle trigger to update current row based on the data of one of the columnOracle触发器根据其中一列的数据更新当前行
【发布时间】:2014-06-16 09:03:15
【问题描述】:

我有一个名为 my_func 的函数,我想传递插入的最新行的一个字段,并使用函数返回的值更新同一行中的另一个字段。

我的 DDL 是,

create table sampletable ( indexid number(19,0) , xmlcolumn xmltype not null , checkfield raw(30) null) xmltype column xmlcolumn store as binary xml;

这是我的插入查询

insert into sample_table values ( 1 , some_xml_document  );

以下是我的触发器,这当然是不正确的

create or replace 
trigger compute_func
after insert on sampletable
for each row
declare 
  returned_value raw(30);
begin
 returned_value := my_func(sampleable.xmlcolumn);
 update sampletrigger set checkfield = returned_value; 
end;

【问题讨论】:

    标签: oracle function triggers oracle11g xmltype


    【解决方案1】:

    不要使用update,只需分配值。此外,您需要使用BEFORE 触发器,因为您无法更改AFTER 触发器中的列值。

    create or replace trigger compute_func
       before insert on sampletable
    for each row
    begin
     :new.checkfield := my_func(:new.xmlcolumn);
    end;
    

    手册中的更多详细信息:http://docs.oracle.com/cd/E11882_01/appdev.112/e25519/triggers.htm#LNPLS655

    【讨论】:

    • 我收到类似错误,错误(2,3):PL/SQL:语句被忽略错误(2,3):PLS-00201:必须声明标识符“NEW.CHECKFIELD”
    • @NishanthLawrence:为我工作:sqlfiddle.com/#!4/571f3/1(请注意,触发器必须更改为 BEFORE INSERT 触发器 - 您使用的是 AFTER INSERT 触发器)
    • 我之前也使用过同样的错误,顺便说一下,在你的 sqlfiddle 中你只是在修改同一个字段。我正在尝试根据 xmltype 字段的值设置一个字段
    • @NishanthLawrence:错误PLS-00201: identifier 'NEW.CHECKFIELD' must be declared 要么表示您的表没有列名checkfield,要么您忘记了NEW 前面的冒号:。它必须是 :new.checkfield 而不是 new.checkfield。该错误与调用函数无关
    • 你说得对,我错过了冒号,但我希望在插入后执行触发器。因为我想在插入后访问 xmlcolumn
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-17
    • 2021-07-15
    • 1970-01-01
    • 2017-11-08
    • 1970-01-01
    • 2016-06-11
    相关资源
    最近更新 更多