触发器正在忽略PL / SQL语句错误

时间:2014-04-25 09:41:47

标签: sql oracle plsql syntax-error

我有这个触发器,我使用case语句对其进行了修改,但它给出了错误:

create or replace TRIGGER trg_stg_timeattendance
  AFTER INSERT OR UPDATE ON co_conf_em_tm_enr
  REFERENCING NEW AS NEW
  FOR EACH ROW
DECLARE
  pragma autonomous_transaction;
  --Variable declaration for stg_pos_ta - AS400
  l_induction_fl CHAR(1) := 'N';
  l_error_code   VARCHAR2(10);
  l_error_messg  VARCHAR2(100);

  --Variable declaration for stg_ta_payroll - Lotus Notes
  V_TA_DATE         NUMBER(6) := null;
  V_TIME_IN         NUMBER(4) := null;
  V_TIME_OUT        NUMBER(4) := null;
  V_MGR_OVRD        char(1) := 'N';
  V_AREA            VARCHAR2(20) := null;
  V_ASSC_NAME       VARCHAR2(60) := null;
  V_MISC_WAGES_DESC VARCHAR2(30) := null;
  V_SHRT_STR_NAME   VARCHAR2(50) := null;
  V_SESSION_NO      NUMBER(2) := 0;
  V_OIM_ENABLED     CHAR(1);

BEGIN
  ----Merging into stg_pos_ta - AS400 - Start
  BEGIN
    SELECT decode(nvl(commission_percentage, 0), 0, 'Y', 'N')
      INTO l_induction_fl
      FROM ebiz_user_cache
     WHERE employee_id = :NEW.ID_EM;
  EXCEPTION
    WHEN NO_DATA_FOUND THEN
      l_induction_fl := 'N';
  END;

  IF (:NEW.FL_CONF = '1') THEN
    MERGE into stg_pos_ta
    USING (select :NEW.ID_TM_ENR ID_TM_ENR,
                  :NEW.ID_EM ID_EM,
                  :NEW.ID_MGR ID_MGR,
                  to_number(:NEW.ID_STR_RT) ID_STR_RT,
                  :NEW.TS_EM_TM_IN TS_EM_TM_IN,
                  :NEW.TS_EM_TM_OUT TS_EM_TM_OUT,
                  :NEW.HRS_STR HRS_STR,
                  :NEW.OTHER_HRS_STR OTHER_HRS_STR,
                  :NEW.TY_TM TY_TM,
                  :NEW.FL_CONF FL_CONF,
                  :NEW.CD_MGR_RSN CD_MGR_RSN,
                  :NEW.FL_MOD FL_MOD,
                  :NEW.FL_DEL FL_DEL,
                  l_induction_fl EMP_INDUCTION_FL,
                  :NEW.TS_CRT_RCRD TS_CRT_RCRD,
                  :NEW.TS_MDF_RCRD TS_MDF_RCRD
             from dual) STG
    ON (TIME_ENT_ID = :NEW.ID_TM_ENR AND to_number(STR_ID) = to_number(:NEW.ID_STR_RT))
    WHEN MATCHED THEN
      UPDATE
         SET EMP_ID               = STG.ID_EM,
             MGR_ID               = STG.ID_MGR,
             EMP_TM_IN            = STG.TS_EM_TM_IN,
             EMP_TM_OUT           = STG.TS_EM_TM_OUT,
             TOT_HRS              = STG.HRS_STR,
             TOT_HRS_OTHRS        = STG.OTHER_HRS_STR,
             TIME_TYP             = STG.TY_TM,
             CONFIRM_MGR_FL       = STG.FL_CONF,
             MGR_RSN_CD           = STG.CD_MGR_RSN,
             TM_MODIFIED_FL       = STG.FL_MOD,
             TM_DELETED_FL        = STG.FL_DEL,
             EMP_INDUCTION_FL     = STG.EMP_INDUCTION_FL,
             TS_MDF_RCRD          = STG.TS_MDF_RCRD,
             STG_OSB_STATUS       = 0,
             STG_OSB_PROCESS_DATE = NULL
    WHEN NOT MATCHED THEN
      INSERT
        (TIME_ENT_ID,
         EMP_ID,
         MGR_ID,
         STR_ID,
         EMP_TM_IN,
         EMP_TM_OUT,
         TOT_HRS,
         TOT_HRS_OTHRS,
         TIME_TYP,
         CONFIRM_MGR_FL,
         MGR_RSN_CD,
         TM_MODIFIED_FL,
         TM_DELETED_FL,
         EMP_INDUCTION_FL,
         TS_CRT_RCRD,
         TS_MDF_RCRD,
         STG_EVENT_ID,
         STG_OSB_STATUS,
         STG_OSB_ERROR_MESSAGE,
         STG_LOAD_DATE,
         STG_OSB_PROCESS_DATE)
      VALUES
        (STG.ID_TM_ENR,
         STG.ID_EM,
         STG.ID_MGR,
         to_number(STG.ID_STR_RT),
         STG.TS_EM_TM_IN,
         STG.TS_EM_TM_OUT,
         STG.HRS_STR,
         STG.OTHER_HRS_STR,
         STG.TY_TM,
         STG.FL_CONF,
         STG.CD_MGR_RSN,
         STG.FL_MOD,
         STG.FL_DEL,
         l_induction_fl,
         STG.TS_CRT_RCRD,
         STG.TS_MDF_RCRD,
         seq_stg_event_id.NEXTVAL,
         0,
         NULL,
         SYSDATE,
         NULL);
    COMMIT;
    ----Merging into stg_pos_ta - AS400 - End

    ----Changes for Merging into stg_ta_payroll - Lotus Notes - Start
    --Manager Override flag
    IF ((:NEW.TY_TM = '0' and :NEW.ID_MGR IS NOT NULL) OR
       (:NEW.TY_TM != '0')) THEN
      V_MGR_OVRD := 'Y';
    END IF;

    --Session Number
    If (:NEW.TY_TM = '0') then
      SELECT COUNT(*)
        INTO V_SESSION_NO
        FROM CO_CONF_EM_TM_ENR
       WHERE ID_EM = :NEW.ID_EM
         AND trunc(TS_EM_TM_IN) = trunc(:NEW.TS_EM_TM_IN)
         AND TY_TM = '0'
         AND FL_CONF = '1';

      SELECT V_SESSION_NO + 1 INTO V_SESSION_NO FROM DUAL;
    else
      SELECT COUNT(*)
        INTO V_SESSION_NO
        FROM CO_CONF_EM_TM_ENR
       WHERE ID_EM = :NEW.ID_EM
         AND trunc(TS_EM_TM_IN) = trunc(:NEW.TS_EM_TM_IN)
         AND TY_TM != '0'
         AND FL_CONF = '1';

      SELECT V_SESSION_NO + 50 INTO V_SESSION_NO FROM DUAL;
    end if;

    --Area Code for Store
    BEGIN
      SELECT nvl(ID_NODE_PRNT, 0)
        INTO V_AREA
        FROM EBIZ_STG_STORE_HIERARCHY_CACHE
       WHERE to_number(ID_NODE) = to_number(:NEW.ID_STR_RT);
    EXCEPTION
      WHEN NO_DATA_FOUND THEN
        V_AREA := null;
    END;

    --Associate Name
    BEGIN
      SELECT LKUP_VAL
        INTO V_OIM_ENABLED
        FROM SH_EBIZ_LOOKUP
       WHERE LKUP_CD = 'OIM_ENABLED'
         AND LKUP_TYPE = 'SYSTEM_CONFIG';

      IF (V_OIM_ENABLED = 'Y') THEN
        SELECT FIRST_NAME || ' ' || LAST_NAME
          INTO V_ASSC_NAME
          FROM EBIZ_USER_CACHE
         WHERE EMPLOYEE_ID = :NEW.ID_EM;
      ELSE
        SELECT FN_EM || ' ' || LN_EM
          INTO V_ASSC_NAME
          FROM NBTY_STORE_EMPLOYEE
         WHERE ID_EM = :NEW.ID_EM;
      END IF;
    EXCEPTION
      WHEN NO_DATA_FOUND THEN
        V_ASSC_NAME := null;
    END;

    --Misc Wages description
    BEGIN
      SELECT LKUP_VAL
        INTO V_MISC_WAGES_DESC
        FROM SH_EBIZ_LOOKUP
       WHERE LKUP_TYPE = 'PAYROLL_MISC_WAGES'
         AND LKUP_CD = :NEW.TY_TM;
    EXCEPTION
      WHEN NO_DATA_FOUND THEN
        V_MISC_WAGES_DESC := null;
    END;

    --Short Store Name
    BEGIN
      SELECT TRIM(SUBSTR(NM_LEVEL, 1, INSTR(NM_LEVEL, '(') - 1))
        INTO V_SHRT_STR_NAME
        FROM EBIZ_STG_STORE_HIERARCHY_CACHE
       WHERE to_number(ID_NODE) = to_number(:NEW.ID_STR_RT);
    EXCEPTION
      WHEN NO_DATA_FOUND THEN
        V_SHRT_STR_NAME := null;
    END;

    MERGE INTO stg_ta_payroll
    USING (select :NEW.ID_TM_ENR ID_TM_ENR,
                  to_number(:NEW.ID_STR_RT) ID_STR_RT,
                  :NEW.ID_EM ID_EM,
                  nvl(TO_NUMBER(TO_CHAR(:NEW.TS_EM_TM_IN, 'YYMMDD')), 0) V_TA_DATE,
                  V_SESSION_NO V_SESSION_NO,
                  :NEW.TS_EM_TM_IN TS_EM_TM_IN,
                  :NEW.TS_EM_TM_OUT TS_EM_TM_OUT,
                  (case
                    when :NEW.TY_TM != '0' then
                     :NEW.TS_EM_TM_IN = ''
                    else
                     nvl(to_char(:NEW.TS_EM_TM_IN, 'HH24MI'), '0000')
                  end) V_TIME_IN,
                  (case
                    when :NEW.TY_TM != '0' then
                     :NEW.TS_EM_TM_OUT = ''
                    else
                     nvl(to_char(:NEW.TS_EM_TM_OUT, 'HH24MI'), '0000')
                  end) V_TIME_OUT,
                  V_MGR_OVRD V_MGR_OVRD,
                  :NEW.ID_MGR ID_MGR,
                  DECODE(:NEW.TY_TM, 'XX', '9', :NEW.TY_TM) TY_TM,
                  :NEW.OTHER_HRS_STR OTHER_HRS_STR,
                  V_AREA V_AREA,
                  V_ASSC_NAME V_ASSC_NAME,
                  V_MISC_WAGES_DESC V_MISC_WAGES_DESC,
                  V_SHRT_STR_NAME V_SHRT_STR_NAME,
                  (:NEW.HRS_STR + :NEW.OTHER_HRS_STR) TOT_HRS
             from dual) STG
    ON (TIME_ENT_ID = STG.ID_TM_ENR AND to_number(STR_ID) = to_number(STG.ID_STR_RT))
    WHEN MATCHED THEN
      UPDATE
         SET STATUS  = null,
             EMP_ID  = STG.ID_EM,
             TA_DATE = STG.V_TA_DATE,
             --SESSION_NO = STG.V_SESSION_NO,
             TIME_IN              = STG.V_TIME_IN,
             TIME_OUT             = STG.V_TIME_OUT,
             MGR_OVERRIDE         = STG.V_MGR_OVRD,
             MGR_EMP_NO           = STG.ID_MGR,
             MISC_WAGES_CODE      = STG.TY_TM,
             MISC_WAGES_HRS       = STG.OTHER_HRS_STR,
             AREA                 = STG.V_AREA,
             ASSOCIATE_NAME       = STG.V_ASSC_NAME,
             MISC_WAGES_DESC      = STG.V_MISC_WAGES_DESC,
             SHRT_STR_NAME        = STG.V_SHRT_STR_NAME,
             HRS                  = STG.TOT_HRS,
             STG_OSB_PROCESS_DATE = null
    WHEN NOT MATCHED THEN
      INSERT
        (TIME_ENT_ID,
         STATUS,
         STR_ID,
         EMP_ID,
         TA_DATE,
         SESSION_NO,
         TIME_IN,
         TIME_OUT,
         MGR_OVERRIDE,
         MGR_EMP_NO,
         MISC_WAGES_CODE,
         MISC_WAGES_HRS,
         AREA,
         ASSOCIATE_NAME,
         MISC_WAGES_DESC,
         SHRT_STR_NAME,
         HRS,
         STG_LOAD_DATE,
         STG_EVENT_ID,
         STG_OSB_STATUS)
      VALUES
        (STG.ID_TM_ENR,
         null,
         STG.ID_STR_RT,
         STG.ID_EM,
         STG.V_TA_DATE,
         STG.V_SESSION_NO,
         STG.V_TIME_IN,
         STG.V_TIME_OUT,
         STG.V_MGR_OVRD,
         STG.ID_MGR,
         STG.TY_TM,
         STG.OTHER_HRS_STR,
         STG.V_AREA,
         STG.V_ASSC_NAME,
         STG.V_MISC_WAGES_DESC,
         STG.V_SHRT_STR_NAME,
         STG.TOT_HRS,
         SYSDATE,
         seq_stg_event_id.NEXTVAL,
         0);
    COMMIT;
  END if;
  ----Changes for Merging into stg_ta_payroll - Lotus Notes - End
EXCEPTION
  WHEN OTHERS THEN
    UPDATE co_conf_em_tm_enr
       SET id_tlog_btch = 1,
           btch_msg_dsc = 'Error inserting stg_pos_commission - ' ||
                          l_error_code || ' - ' || l_error_messg
     WHERE id_em = :NEW.ID_EM
       AND To_Char(ts_em_tm_in, 'dd-mm-yyyy') =
           To_Char(:NEW.TS_EM_TM_IN, 'dd-mm-yyyy');
    COMMIT;
END trg_stg_timeattendance;

我在using关键字上收到错误,该错误表示忽略了SQL语句,而case语句上的另一个错误表示错误的关键字错误。

2 个答案:

答案 0 :(得分:1)

CASE表达式格式错误。让我们考虑以下几点:

(case
   when :NEW.TY_TM != '0' then
     :NEW.TS_EM_TM_IN = ''
   else
     nvl(to_char(:NEW.TS_EM_TM_IN, 'HH24MI'), '0000')
   end) V_TIME_IN

"当" branch尝试返回BOOLEAN(它不是SQL中的合法数据类型),但是" else" branch返回一个字符串。这是不允许的 - CASE表达式中所有分支返回的值必须是相同的类型,或者为NULL。

分享并享受。

编辑: 现在看到您关于用DECODE替换CASE的评论 - 您可以通过执行以下操作从CASE表达式获得相同的结果:

CASE
  WHEN :NEW.TY_TM = '0' THEN
    nvl(to_char(:NEW.TS_EM_TM_IN,'HH24MI'),'0000')
  ELSE
    NULL
END AS V_TIME_IN

答案 1 :(得分:0)

使用了decode而不是case语句。

DECODE(:NEW.TY_TM,' 0',NVL(TO_CHAR(:NEW.TS_EM_TM_IN,' HH24MI'),' 0000'),&# 39;')V_TIME_IN,         解码(:NEW.TY_TM,' 0',nvl(to_char(:NEW.TS_EM_TM_OUT,' HH24MI'),' 0000'),'& #39;)V_TIME_OUT,