sqloracle-databaseif-statementtriggersin-operator

IF condition containing IN operator inside ORACLE Trigger is not working


I have a trigger that uses IF condition with IN operator and two variables v_audit_user and v_evdnse_user inside IN. Both variables are containing comma separated ID values. The trigger gets compiled successfully with no errors. I am not understanding why the IF condition with IN is not working. When I select the function that assigns value to the variables independently, I do see the comma separated values, so nothing is wrong with function (see screenshot).

Coomma Separated Values

create or replace TRIGGER TRG_CHK_HRCQA_CASE_ACTIONS
AFTER INSERT ON KDD_CASE_ACTIONS
FOR EACH ROW
DECLARE

 user_audit kdd_review_owner.OWNER_ID%TYPE; /* The user that is displayed in audit */
 user_evdnse kdd_review_owner.OWNER_ID%TYPE; /* The user that took action in evidence tab */
 v_audit_user NUMBER;  /* The HRCO/QA user from audit tab */
 v_evdnse_user NUMBER; /* The HRCO/QA user from evidence tab */
 LV_ERRORCODE VARCHAR2(1000);

BEGIN

  /* pass the username into the variables */

    SELECT kro.OWNER_ID into user_audit from kdd_review_owner kro where kro.OWNER_SEQ_ID = :NEW.ACTION_BY_ID;

    SELECT kro.OWNER_ID into user_evdnse from kdd_review_owner kro where kro.OWNER_SEQ_ID = :NEW.ACTION_BY_ID;

  /* fetch the comma separated IDs */
  
   v_audit_user := F_GET_HRCQA_ACTIONS(:NEW.CASE_INTRL_ID,user_audit,'AUDIT');

   v_evdnse_user := F_GET_HRCQA_ACTIONS(:NEW.CASE_INTRL_ID,user_evdnse,'EVDNSE');


  --  select ENTITY_ID into v_evdnse_user from table(f_get_arg_table(F_GET_HRCQA_ACTIONS(:NEW.CASE_INTRL_ID,user_evdnse,'EVDNSE')));
  /* If the action taken is by QA or HRCO role */

    IF (:NEW.ACTION_SEQ_ID in (v_audit_user,v_evdnse_user)) 

    THEN

  /* then insert record in the SC_HRCQA_CASE_ACTIONS table with IS_HRCO_QA flag as Y  */

        Insert into SC_HRCQA_CASE_ACTIONS (ACTION_SEQ_ID,ACTION_BY_ID,ACTION_TS,STATUS_CD,CASE_INTRL_ID,ACTION_ID,NEW_CASE_OWNR_ASSGN_ID,CASE_DUE_TS,PREV_CASE_OWNR_ASSGN_ID,IS_HRCO_QA) 
        values (:NEW.ACTION_SEQ_ID, :NEW.ACTION_BY_ID, :NEW.ACTION_TS, :NEW.STATUS_CD, :NEW.CASE_INTRL_ID, :NEW.ACTION_ID, :NEW.NEW_CASE_OWNR_ASSGN_ID, :NEW.CASE_DUE_TS, :NEW.PREV_CASE_OWNR_ASSGN_ID,'Y');

--        ELSE
--
--  /* else the logged in user is NOT HRCO/QA hence insert record in the SC_HRCQA_CASE_ACTIONS table with IS_HRCO_QA flag as N  */   
--
--        Insert into SC_HRCQA_CASE_ACTIONS (ACTION_SEQ_ID,ACTION_BY_ID,ACTION_TS,STATUS_CD,CASE_INTRL_ID,ACTION_ID,NEW_CASE_OWNR_ASSGN_ID,CASE_DUE_TS,PREV_CASE_OWNR_ASSGN_ID,IS_HRCO_QA) 
--        values (:NEW.ACTION_SEQ_ID, :NEW.ACTION_BY_ID, :NEW.ACTION_TS, :NEW.STATUS_CD, :NEW.CASE_INTRL_ID, :NEW.ACTION_ID, :NEW.NEW_CASE_OWNR_ASSGN_ID, :NEW.CASE_DUE_TS, :NEW.PREV_CASE_OWNR_ASSGN_ID,'N');

    END IF;

EXCEPTION
    WHEN OTHERS THEN LV_ERRORCODE := SQLCODE;
        INSERT INTO KDD_LOGS_MSGS (LOG_DT, LOG_INFO_TX, REMARK_TX)
        VALUES (SYSDATE,'ErrorCode - ' || LV_ERRORCODE,'TRG_CHK_HRCQA_CASE_ACTIONS');
END;

Solution

  • You cannot pass a comma-delimited string stored in a single variable to an IN condition and expect it to be parsed as multiple values as it is not.

    If you want to use a single variable containing a delimited list then you will need to use string functions to find a sub-string match:

    create or replace TRIGGER TRG_CHK_HRCQA_CASE_ACTIONS
      AFTER INSERT ON KDD_CASE_ACTIONS
      FOR EACH ROW
    DECLARE
      v_owner_id    kdd_review_owner.OWNER_ID%TYPE;
      v_audit_user  VARCHAR2(1000);
      v_evdnse_user VARCHAR2(1000);
      LV_ERRORCODE  VARCHAR2(1000);
    BEGIN
      SELECT OWNER_ID
      into   v_owner_id                         -- You only need one variable here
      from   kdd_review_owner
      where  OWNER_SEQ_ID = :NEW.ACTION_BY_ID;
    
      v_audit_user := F_GET_HRCQA_ACTIONS(:NEW.CASE_INTRL_ID, v_owner_id, 'AUDIT');
      v_evdnse_user := F_GET_HRCQA_ACTIONS(:NEW.CASE_INTRL_ID, v_owner_id, 'EVDNSE');
    
      IF ','||v_audit_user||','||v_evdnse_user||',' LIKE '%,'||:NEW.ACTION_SEQ_ID||',%'
      THEN
         Insert into SC_HRCQA_CASE_ACTIONS (
           ACTION_SEQ_ID, ACTION_BY_ID, ACTION_TS, STATUS_CD, CASE_INTRL_ID,
           ACTION_ID, NEW_CASE_OWNR_ASSGN_ID, CASE_DUE_TS, PREV_CASE_OWNR_ASSGN_ID, IS_HRCO_QA
         ) values (
           :NEW.ACTION_SEQ_ID, :NEW.ACTION_BY_ID, :NEW.ACTION_TS, :NEW.STATUS_CD, :NEW.CASE_INTRL_ID,
           :NEW.ACTION_ID, :NEW.NEW_CASE_OWNR_ASSGN_ID, :NEW.CASE_DUE_TS, :NEW.PREV_CASE_OWNR_ASSGN_ID,'Y'
         );
      END IF;
    EXCEPTION
        WHEN OTHERS THEN LV_ERRORCODE := SQLCODE;
            INSERT INTO KDD_LOGS_MSGS (LOG_DT, LOG_INFO_TX, REMARK_TX)
            VALUES (SYSDATE,'ErrorCode - ' || LV_ERRORCODE,'TRG_CHK_HRCQA_CASE_ACTIONS');
    END;
    /