2015-08-28 42 views
0

我有下面的觸發器,我想在FIELD_TRACKING表中插入值。 我有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; 

回答

2

你可以使用一個嵌套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 
); 

雖然persionally我會使用null代替'',和您可以省略else null子句,因爲這是默認設置。看起來奇怪的是,您的外部IF條件將NVL的旗標值設置爲0,因爲它似乎是Y/N的字符列。

+0

好吧,我用你的答案替換這個插入和它的說法列不允許在Last_Updated_By的地方在case語句 – Andrew

+1

@rahul - 是的,我複製了你放的東西,但沒有足夠的思考。你想要那個專欄的舊價值或新價值嗎?我更新了使用':new'的答案,因爲這似乎是您檢查內容的明顯選擇,但如果您願意,可以使用':old'。 –

+0

感謝亞歷克斯現在的工作:) – Andrew