【问题标题】:I have an error with trigger using if conditional and update我在使用 if 条件和更新时触发错误
【发布时间】:2018-12-09 03:12:33
【问题描述】:

在 ORACLE 中执行此触发器出现以下错误:

 Table, View Or Sequence reference 'OCEX_COMI.FECHA_ASIG_GT' not allowed in this context.

这是我的触发器:

create or replace trigger ocex_comi_total
before insert or update of id_gt,fecha_asig_gt on ocex_comi
for each row
begin
if ((ocex_comi.fecha_asig_gt > to_date('2018-12-15','yyyy-mm-dd')) and 
 (ocex_comi.fecha_asig_gt < to_date('2019-01-01','yyyy-mm-dd'))) 
then
update ocex_comi cm set
cm.PAGO_COM = (select uea.total_bono_especial from OCEX_UEA uea                   
                join OCEX_GUIA_TRANSITO gt on uea.id_uea = gt.dest_id
                where gt.cod_gt=cm.id_gt)
where cm.id_gt = (select gt.cod_gt from ocex_guia_transito gt JOIN 
                  ocex_uea uea on uea.id_uea=gt.dest_id 
                        where gt.cod_gt=cm.id_gt);
else
update ocex_comi cm set
cm.PAGO_COM = (select uea.total_x_pnp from OCEX_UEA uea                   
                join OCEX_GUIA_TRANSITO gt on uea.id_uea = gt.dest_id
                where gt.cod_gt=cm.id_gt)
where cm.id_gt = (select gt.cod_gt from ocex_guia_transito gt JOIN 
                  ocex_uea uea on uea.id_uea=gt.dest_id 
                        where gt.cod_gt=cm.id_gt);
end if;
end;

好吧,我试图做的是,使用此触发器,表“ocex_comi”的列“PAGO_COM”会自动从表“ocex_uea”中填充,这要归功于“total_bono_special”列(如果“date_asig_gt”字段包括)在 15 日至 31 日之间),如果没有,请填写“total_x_pnp”栏(如果“fecha_asig_gt”字段的日期不在 15 日至 31 日之间。) 一些想法或帮助解决我遇到的错误,谢谢。

【问题讨论】:

    标签: oracle plsql oracle11g


    【解决方案1】:

    不要直接引用表格列;你需要参考the new pseudorecord

    if ((:new.fecha_asig_gt > to_date('2018-12-15','yyyy-mm-dd')) and 
     (:new.fecha_asig_gt < to_date('2019-01-01','yyyy-mm-dd'))) 
    then
    

    虽然我会使用日期文字:

    if :new.fecha_asig_gt > date '2018-12-15' and 
     :new.fecha_asig_gt < date '2019-01-01'
    then
    

    (不确定你是否真的想要&gt;= 而不是&gt;。)

    但是您还尝试更新逻辑的每个分支内的表中的所有行,这听起来并不是您真正想要做的事情,如果您尝试,这将在运行时导致变异表错误。

    目前还不太清楚你的意图是什么,但我认为你想要的更像是:

    ...
    then
      select uea.total_bono_especial
      into :new.PAGO_COM
      from OCEX_UEA uea                   
      join OCEX_GUIA_TRANSITO gt on uea.id_uea = gt.dest_id
      where gt.cod_gt = :new.id_gt;
    else
      ...
    

    和另一个分支中的相同类型的东西。

    由于这些查询非常相似,您可以将 if/else 逻辑替换为单个查询,该查询使用 case 表达式来决定要返回两个列值中的哪一个。

    【讨论】:

    • 感谢您的帮助,最后当我运行触发器时,它成功了。另一方面,在您建议更改我的选择结构的部分,oracle 检测到类似错误并显示消息“innapppropiate into”,我尝试对触发器执行的操作是仅更新“pago_com”中的字段“ocex_comi”表
    • 这就是东。 select 完全取代了您的 update,而不仅仅是您的子查询,如果您尝试使用它的话?
    • 不,我唯一的想法是当ocex_comi表的id“gg_id”和ocex_guia_transito表的“cod_gt”重合时,“pago_com”字段会自动填写,因为ocex_guia_transito表已经使用“id_uea”链接到“ocex_uea”表。对了,我忘了提,我只是在学习Oracle,以防我做很多可能可以简化的代码。
    猜你喜欢
    • 2017-03-13
    • 2017-11-30
    • 2016-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-13
    • 1970-01-01
    相关资源
    最近更新 更多