解码函数中的多个条件以在 table 中插入值

Multiple condition in decode function to insert value in table

我有下面的触发器,我想在其中插入值 FIELD_TRACKING table。 我有 FIELD_TRACKING_COMMENTS 字段,我想根据条件在其中插入值。条件是如果 (Last_Updated_By = 115 and new.KPI_ACTIVE_MANUAL,N) 那么注释应该是 This KPI has been deactivated by System -> Active Alerts。我已经在解码语句 decode(:new.KPI_ACTIVE_MANUAL,'N','This KPI has been deactivated by System -> Active Alerts','' )) 中为 FIELD_TRACKING_COMMENTS 字段设置了一个条件并且它工作正常。但我想添加额外的条件'Last_Updated_By = 115',我不知道如何在解码语句中添加它。

create or replace TRIGGER RATOR_MONITORING_CONFIGURATION."TRG_TRK_KPI_DEFINITION" AFTER UPDATE ON RATOR_MONITORING_CONFIGURATION.KPI_DEFINITION 
  FOR EACH ROW   
BEGIN
    ----New change
  --IF NOT  :old.KPI_ACTIVE_MANUAL=:new.KPI_ACTIVE_MANUAL THEN
  IF NOT NVL(:old.KPI_ACTIVE_MANUAL,0)=NVL(:new.KPI_ACTIVE_MANUAL,0) THEN
    INSERT INTO RATOR_MONITORING_CONFIGURATION.FIELD_TRACKING  (TABLE_ID, FIELD_NAME,FIELD_OLD_VALUE,FIELD_NEW_VALUE,USER_ID, FIELD_TRACKING_COMMENTS)
    VALUES (:new.KPI_DEF_ID,'Active(Manual)',NVL(to_char(:old.KPI_ACTIVE_MANUAL),'Y'),NVL(to_char(:new.KPI_ACTIVE_MANUAL),'Y'),:new.LAST_UPDATED_BY,decode(:new.KPI_ACTIVE_MANUAL,'N','This KPI has been deactivated by System -> Active Alerts','' ));
  END IF;

END;

您可以使用嵌套 decode:

decode(:new.KPI_ACTIVE_MANUAL,'N',
  decode(:new.Last_Updated_By, 115,
    'This KPI has been deactivated by System -> Active Alerts',
    ''),
  '')

但使用 case 表达式更简单:

INSERT INTO RATOR_MONITORING_CONFIGURATION.FIELD_TRACKING  (TABLE_ID,
  FIELD_NAME,FIELD_OLD_VALUE,FIELD_NEW_VALUE,USER_ID, FIELD_TRACKING_COMMENTS)
VALUES (:new.KPI_DEF_ID, 'Active(Manual)',
  NVL(to_char(:old.KPI_ACTIVE_MANUAL),'Y'),
  NVL(to_char(:new.KPI_ACTIVE_MANUAL),'Y'),
  :new.LAST_UPDATED_BY,
  case when :new.KPI_ACTIVE_MANUAL = 'N' AND :new.Last_Updated_By = 115
    then 'This KPI has been deactivated by System -> Active Alerts'
    else '' end
);

虽然我个人会使用 null 而不是 '',并且您可以省略 else null 子句,因为这是默认设置。您的外部 IF 条件将标志值 NVL 设置为 0 似乎也很奇怪,因为它似乎是 Y/N.[=21 的字符列=]