我在使用if条件和更新触发器时出错

时间:2018-06-29 22:04:29

标签: oracle plsql oracle11g

在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;

好吧,我要尝试执行的操作是,通过此触发器,由于“ total_bono_special”列(如果是日期,则自动从表“ ocex_uea”中填充表“ ocex_comi”的列“ PAGO_COM”) (包括“ date_asig_gt”字段)(介于12月15日至12月31日之间),如果没有,则填写“ total_x_pnp”列(如果字段“ fecha_asig_gt”的日期不在15 dec至31 Dec之间)。想法或帮助解决我遇到的错误,谢谢。

1 个答案:

答案 0 :(得分:2)

不要直接引用表格列;您需要参考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

(不过,不确定是不是真的要>=而不是>。)

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

目前还不清楚您的意图是什么,但我认为您想要更多类似的东西:

...
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逻辑,wowch使用case表达式来确定要返回两个列值中的哪个。