ORA-01799:列可能没有外部连接到带有insert语句的子查询

时间:2016-10-19 12:50:11

标签: sql oracle plsql

我正在尝试创建此过程:

CREATE OR REPLACE PROCEDURE USP_DAILY_REP_REG_REFRESH
IS
BEGIN

  SAVEPOINT start_tran;
  EXECUTE immediate 'truncate table rep_fir_acts';

  --  Insert Query
  INSERT
  INTO rep_fir_acts
    (
      PS_CD,
      LANG_CD,
      FIR_REG_NUM,
      FIR_DT,
      ACT_CD,
      PEND_INV_FIR_NUM,
      FINAL_REP_SRNO,
      CHARGESHEET_DT,
      INVTG_STATUS_CD,
      FR_TYPE_CD,
      FINAL_REPORT_TYPE_CD,
      PEND_TRIAL_FIR,
      DISPOSAL_NATURE_CD,
      ACTION_TAKEN_CD,
      FIR_STATUS
    )
  SELECT DISTINCT F.PS_CD,
    F.LANG_CD,
    F.FIR_REG_NUM,
    F.REG_DT FIR_DT,
    C.ACT_CD ,
    fnl.FIR_REG_NUM PEND_INV_FIR_NUM,
    fnl.FINAL_REP_SRNO ,
    fnl.CHARGESHEET_DT,
    fnl.INVTG_STATUS_CD,
    fnl.FR_TYPE_CD,
    fnl.FINAL_REPORT_TYPE_CD ,
    dis.FIR_REG_NUM PEND_TRIAL_FIR,
    dis.DISPOSAL_NATURE_CD ,
    f.ACTION_TAKEN_CD,
    f.FIR_STATUS
  FROM t_fir_registration f
  INNER JOIN t_crime_act_section c
  ON f.fir_reg_num = c.fir_reg_num
  AND f.lang_cd    = c.lang_cd
  INNER JOIN t_crime_detail cd
  ON c.fir_reg_num      = cd.fir_reg_num
  AND c.lang_cd         = cd.lang_cd
  AND c.CRM_DETAIL_SRNO = cd.CRM_DETAIL_SRNO
  AND cd.RECORD_STATUS  = 'C'
  LEFT JOIN t_final_report fnl
  ON f.fir_reg_num            = fnl.fir_reg_num
  AND f.lang_cd               = fnl.lang_cd
  AND IS_CHARGESHEET_ORIGINAL = 'Y'
  AND fnl.final_rep_srno     IN
    (SELECT MAX(final_rep_srno)
    FROM t_final_report fn
    WHERE f.fir_reg_num            = fn.fir_reg_num
    AND f.lang_cd                  = fn.lang_cd
    AND fn.IS_CHARGESHEET_ORIGINAL = 'Y'
    )
  LEFT JOIN t_court_disposal dis
  ON f.fir_reg_num       = dis.fir_reg_num
  AND dis.DISPOSAL_SRNO IN
    (SELECT MAX(DISPOSAL_SRNO)
    FROM t_court_disposal cdis
    WHERE dis.fir_reg_num = cdis.fir_reg_num
    )
  WHERE c.act_cd       <> 0
  AND f.fir_status NOT IN(2,20,104,103);
EXCEPTION
WHEN OTHERS THEN
  ROLLBACK TO start_tran;
  RAISE;
END;

当我尝试运行时,我得到:

  PL/SQL: SQL Statement ignored
  ORA-01799: a column may not be outer-joined to a subquery with insert
  statement               

如何解决此问题?

1 个答案:

答案 0 :(得分:1)

您需要将您要加入的内容转换为子查询,而不是将子查询转换为连接条件。例如:

SELECT DISTINCT f.ps_cd,
            f.lang_cd,
            f.fir_reg_num,
            f.reg_dt fir_dt,
            c.act_cd,
            fnl.fir_reg_num pend_inv_fir_num,
            fnl.final_rep_srno,
            fnl.chargesheet_dt,
            fnl.invtg_status_cd,
            fnl.fr_type_cd,
            fnl.final_report_type_cd,
            dis.fir_reg_num pend_trial_fir,
            dis.disposal_nature_cd,
            f.action_taken_cd,
            f.fir_status
FROM   t_fir_registration f
       INNER JOIN t_crime_act_section c
         ON     f.fir_reg_num = c.fir_reg_num
         AND    f.lang_cd = c.lang_cd
       INNER JOIN t_crime_detail cd
         ON     c.fir_reg_num = cd.fir_reg_num
         AND    c.lang_cd = cd.lang_cd
         AND    c.crm_detail_srno = cd.crm_detail_srno
         AND    cd.record_status = 'C'
       LEFT JOIN (SELECT fir_reg_num,
                         lang_cd,
                         final_rep_srno,
                         chargesheet_dt,
                         invtg_status_cd,
                         fr_type_cd,
                         final_report_type_cd
                  FROM   (SELECT fnl1.*,
                                 MAX(fnl1.final_rep_srno) OVER (PARTITION BY fnl1.fir_reg_num, fnl1.lang_cd) max_final_rep_srno
                          FROM   t_final_report fnl1
                          WHERE  fnl1.is_chargesheet_original = 'Y')
                  WHERE  final_rep_srno = max_final_rep_srno) fnl
         ON     f.fir_reg_num = fnl.fir_reg_num
         AND    f.lang_cd = fnl.lang_cd
       LEFT JOIN (SELECT fir_reg_num,
                         disposal_nature_cd
                  FROM   (SELECT dis1.*,
                                 MAX(disposal_srno) OVER (PARTITION BY dis1.fir_reg_num) max_disposal_srno
                          FROM   t_court_disposal dis1)
                  WHERE  disposal_srno = max_disposal_srno) dis
         ON     f.fir_reg_num = dis.fir_reg_num
WHERE  c.act_cd <> 0
AND    f.fir_status NOT IN (2, 20, 104, 103);