oracle update trigger使用case / if

时间:2017-11-17 13:52:22

标签: oracle triggers

我想更新一个状态类似的表。应使用三个交通灯图片显示状态。我成功地创建了一个视图,根据其他属性填充相应的交通灯图片(文件路径)。 ( ampel_rot.jpg 是红色交通信号灯, ampel_gelb.jpg 是黄色交通灯,* ampel_gruen.jpg是绿色交通信号灯)

case when (L.STATUS = 'Aktiv' AND (((select count(*) from INFOVERSAND_FILES I where I.FID_BAUGESUCH=L.FID) = 0) AND ((select count(*) from ENTWAESSERUNGSPLAN_FILES E where E.FID_BAUGESUCH=L.FID)= 0)  AND ((select count(*) from DOKKTV_FILES K where K.FID_BAUGESUCH=L.FID) = 0) AND L.ABWASSER_EINMASS is NULL AND  L.ABWASSER_ABNAHME is NULL AND  L.PAW is NULL AND  L.DOK_K_TV is NULL AND  L.DICHTHEITSPRUEFUNG is NULL AND  L.SANIERUNGSVERFUEGUNG is NULL AND  L.NACHPRUEFUNG is NULL AND  L.ABSCHLUSS is NULL AND  L.KOMMENTAR is NULL)) then 'C:\Temp\Ampel_rot.jpg' 
when (L.STATUS = 'Aktiv' AND (((select count(*) from INFOVERSAND_FILES I where I.FID_BAUGESUCH=L.FID) > 0) OR ((select count(*) from ENTWAESSERUNGSPLAN_FILES E where E.FID_BAUGESUCH=L.FID)> 0)  OR ((select count(*) from DOKKTV_FILES K where K.FID_BAUGESUCH=L.FID) > 0) OR L.ABWASSER_EINMASS is not NULL OR L.ABWASSER_ABNAHME is not NULL OR L.PAW is not NULL OR L.DOK_K_TV is not NULL OR L.DICHTHEITSPRUEFUNG is not NULL OR L.SANIERUNGSVERFUEGUNG is not NULL OR L.NACHPRUEFUNG is not NULL OR L.ABSCHLUSS is not NULL OR L.KOMMENTAR is not NULL) AND L.ABSCHLUSS is NULL AND L.ABSCHLUSS_ZUSATZ is NULL) then 'C:\Temp\Ampel_gelb.jpg'
when (L.ABSCHLUSS is not NULL OR L.ABSCHLUSS_ZUSATZ is not NULL) then 'C:\Temp\Ampel_gruen.jpg' end as Ampel
from LIEGENSCHAFT L

不幸的是我直接在表中需要这些traffic-light-filepath,如 视图是只读的。 所以我试着写另一个更新触发器,到目前为止没有成功:

我试过IF:

create or replace
  TRIGGER LS_STATUS
  before UPDATE ON LIEGENSCHAFT  
  for each row
  DECLARE
  AMP VARCHAR2(50) := 'C:\Temp\Ampel_rot'
  BEGIN
  if :new.STATUS = 'Aktiv' AND (:new.ABWASSER_EINMASS is NULL AND :new.ABWASSER_ABNAHME is NULL AND :new.PAW is NULL AND :new.DOK_K_TV is NULL AND :new.DICHTHEITSPRUEFUNG is NULL AND :new.SANIERUNGSVERFUEGUNG is NULL AND :new.NACHPRUEFUNG is NULL AND :new.ABSCHLUSS is NULL AND :new.KOMMENTAR is NULL)) then AMP:='C:\Temp\Ampel_rot.jpg' else 'leer' END IF;   
  :NEW.AMPEL := AMP;
  END;

并使用案例:

create or replace
  TRIGGER LS_STATUS
  before UPDATE ON LIEGENSCHAFT  
  for each row
  DECLARE
  AMP VARCHAR2(50) := 'C:\Temp\Ampel_rot'
  BEGIN
  AMP := select case when :new.STATUS = 'Aktiv' AND (:new.ABWASSER_EINMASS is NULL AND  :new.ABWASSER_ABNAHME is NULL AND :new.PAW is NULL AND  :new.DOK_K_TV is NULL AND  :new.DICHTHEITSPRUEFUNG is NULL AND  :new.SANIERUNGSVERFUEGUNG is NULL AND  :new.NACHPRUEFUNG is NULL AND  :new.ABSCHLUSS is NULL AND  :new.KOMMENTAR is NULL)) then 'C:\Temp\Ampel_rot.jpg' else 'leer' END;
:NEW.AMPEL := AMP;
END;

我只是试着开始红色交通灯。当这工作时,我可以添加其他定义......

2 个答案:

答案 0 :(得分:0)

哦,你有几个语法错误。如果您格式化代码,它们会更加明显。您也可以尝试使用带语法高亮的IDE,如SQL Developer或TOAD,它们将指出这些问题。

请参阅下面的评论。在第一个:

create or replace
  TRIGGER LS_STATUS
  before UPDATE ON LIEGENSCHAFT  
  for each row
  DECLARE
    AMP VARCHAR2(50) := 'C:\Temp\Ampel_rot' -- missing ; at end
  BEGIN
  if :new.STATUS = 'Aktiv' AND 
    (:new.ABWASSER_EINMASS is NULL 
     AND :new.ABWASSER_ABNAHME is NULL 
     AND :new.PAW is NULL 
     AND :new.DOK_K_TV is NULL 
     AND :new.DICHTHEITSPRUEFUNG is NULL 
     AND :new.SANIERUNGSVERFUEGUNG is NULL 
     AND :new.NACHPRUEFUNG is NULL 
     AND :new.ABSCHLUSS is NULL 
     AND :new.KOMMENTAR is NULL)
     ) -- what's this? no matching left parenthesis.
  then 
     AMP:='C:\Temp\Ampel_rot.jpg' -- missing ;
  else 
     'leer' -- also what's this? It should be "AMP:='leer';"
  END IF;   
  :NEW.AMPEL := AMP;
END;

在第二个:

create or replace
  TRIGGER LS_STATUS
  before UPDATE ON LIEGENSCHAFT  
  for each row
  DECLARE
    AMP VARCHAR2(50) := 'C:\Temp\Ampel_rot' -- missing ; at end
  BEGIN
  AMP := select -- if you want a select, put 'from dual' at the end. but a select isn't needed here.
    case 
    when :new.STATUS = 'Aktiv' 
        AND (:new.ABWASSER_EINMASS is NULL 
            AND  :new.ABWASSER_ABNAHME is NULL 
            AND :new.PAW is NULL 
            AND  :new.DOK_K_TV is NULL 
            AND  :new.DICHTHEITSPRUEFUNG is NULL 
            AND  :new.SANIERUNGSVERFUEGUNG is NULL 
            AND  :new.NACHPRUEFUNG is NULL 
            AND  :new.ABSCHLUSS is NULL 
            AND  :new.KOMMENTAR is NULL)
        ) -- also no matching left paren
    then 'C:\Temp\Ampel_rot.jpg' 
    else 'leer' 
    END;
:NEW.AMPEL := AMP;
END;

答案 1 :(得分:0)

非常感谢您指出我的语法错误以及指向SQL-Developer,这是一个全新的世界! 我纠正了代码并且它现在正在工作。 现在转到我的上一个问题/问题: 我想检查3个相关对象的相关表。在我看来,我可以将三个选择计数放入并过滤它们是否为零。在触发器中以这种方式使用它是否有可能? 这是我在SQL Developer中编译错误的尝试:

create or replace
  TRIGGER LS_STATUS
  before UPDATE ON LIEGENSCHAFT  
  for each row
  DECLARE
    AMP VARCHAR2(50) := 'C:\Temp\Ampel_rot';
  BEGIN
  if :new.STATUS = 'Aktiv' AND 
    (:new.ABWASSER_EINMASS is NULL 
     AND :new.ABWASSER_ABNAHME is NULL 
     AND :new.PAW is NULL 
     AND :new.DOK_K_TV is NULL 
     AND :new.DICHTHEITSPRUEFUNG is NULL 
     AND :new.SANIERUNGSVERFUEGUNG is NULL 
     AND :new.NACHPRUEFUNG is NULL 
     AND :new.ABSCHLUSS is NULL 
     AND :new.KOMMENTAR is NULL
     AND ((select count(*) from INFOVERSAND_FILES I where I.FID_BAUGESUCH=FID) = 0)
     AND ((select count(*) from ENTWAESSERUNGSPLAN_FILES E where E.FID_BAUGESUCH=FID) = 0)
     AND ((select count(*) from DOKKTV_FILES K where K.FID_BAUGESUCH=FID) = 0))
  then 
     AMP:='C:\Temp\Ampel_rot.jpg';
  else
  AMP:='leer'; 
  END IF;   
  :NEW.AMPEL := AMP;
END;

最后它以这种方式工作:

create or replace
  TRIGGER LS_STATUS
  before UPDATE ON LIEGENSCHAFT  
  for each row
  DECLARE
    AMP VARCHAR2(50) := 'C:\Temp\Ampel_rot';
    CSchriftverkehr NUMBER(1);
    CEntwaesserungsplan NUMBER(1);
    CDOKKTV NUMBER(1);
  BEGIN
  select count(*) into CSchriftverkehr from INFOVERSAND_FILES I where I.FID_BAUGESUCH=:new.FID;
  select count(*) into CEntwaesserungsplan from ENTWAESSERUNGSPLAN_FILES E where E.FID_BAUGESUCH=:new.FID;
  select count(*) into CDOKKTV from DOKKTV_FILES K where K.FID_BAUGESUCH=:new.FID;
  if :new.STATUS = 'Aktiv' AND 
    (:new.ABWASSER_EINMASS is NULL 
     AND :new.ABWASSER_ABNAHME is NULL 
     AND :new.PAW is NULL 
     AND :new.DOK_K_TV is NULL 
     AND :new.DICHTHEITSPRUEFUNG is NULL 
     AND :new.SANIERUNGSVERFUEGUNG is NULL 
     AND :new.NACHPRUEFUNG is NULL 
     AND :new.ABSCHLUSS is NULL 
     AND :new.KOMMENTAR is NULL
     AND CSchriftverkehr = 0 AND CEntwaesserungsplan = 0 AND CDOKKTV = 0)
  then 
     AMP:='C:\Temp\Ampel_rot.jpg'; -- missing ;
  else
  AMP:='leer'; 
  END IF;   
  :NEW.AMPEL := AMP;
END;