【问题标题】:Why isn't this CTE recognized?为什么不识别此 CTE?
【发布时间】:2021-04-21 16:28:44
【问题描述】:

我正在尝试通过使用 CTE 将学生专业拉入此 sql,但是当我尝试添加 CTE 字段或使用隐式连接加入 CTE(在其他查询中工作正常)时,oracle 会抛出错误'不合法的识别符'。有什么想法吗?

这只是此 sql 中许多联合的第一部分,但我已经看到 CTE 与联合正常工作的示例,所以我不认为就是这样,除此之外,当我在没有联合的情况下运行此代码时,我得到同样的错误,“无效的标识符”。

with major (pidm, major) as
    (
    select 
        a.sgbstdn_pidm,
        a.sgbstdn_majr_code_1
    from 
        sgbstdn a
    where a.sgbstdn_term_code_eff   = (select
                                        max(b.sgbstdn_term_code_eff)
                                         from
                                            sgbstdn b
                                         where
                                            a.sgbstdn_pidm = b.sgbstdn_pidm
                                            and b.sgbstdn_term_code_eff <= '202004' 
                                            and b.sgbstdn_term_code_eff > '202001')
    )


select --authorized aid
  spriden_id id
  ,spriden_last_name ||', '||spriden_first_name || ' '|| spriden_mi "Name"
  ,major.major "Major"
  ,rpratrm_fund_code "Fund"
  ,rpratrm_period "Period"
  ,rpratrm_offer_amt "Offer"
  ,rpratrm_accept_amt "Accept"
  ,null "Loan Net Orig Amt"
  ,RPRATRM_AUTHORIZE_AMT "Authorized"
  ,rpratrm_paid_amt "Paid"
  ,c.hr "Census Hours"
  ,r.hr "Enrolled Hours"
  ,c.con "Consortium"
  ,b.pbcode "P Budget Code"
  ,b.pbcode_locked "P Budget Code Locked?"
  ,b.b_locked "Budget Locked"
  --,astd.astd "Academic Standing"
  ,s.sap "SAP Code"
  ,s.term "SAP Term"
  ,decode(h.pidm, null, 'No', 'Yes') "Holds"
  ,admit.admit "Admitted?"
from
  spriden
  ,rpratrm
  
  --,(select SGVSTDN_pidm pidm, sgvstdn_astd_desc astd from SGVSTDN where SGVSTDN_term_code = '202003') astd
  
  --admitted?
  ,(select
      sgbstdn_pidm pidm
      ,case 
        when sgbstdn_levl_code like 'N%' then 'No'
        when sgbstdn_levl_code is null then 'No Student Record found this term'
        else 'Yes'
      end admit
    from
      sgbstdn
    where
      sgbstdn_term_code_eff = '202003') admit
  
  --HOLDS
  ,(select
      rorhold_pidm pidm
    from
      rorhold
    where
      to_char(sysdate, 'YYYYMMDD') <= to_char(RORHOLD_TO_DATE, 'YYYYMMDD') 
      and to_char(sysdate, 'YYYYMMDD') >= to_char(RORHOLD_FROM_DATE, 'YYYYMMDD')
      ) h
   
   --SAP   
  ,(select
      a.rorsapr_pidm pidm
      ,a.rorsapr_term_code term
      ,a.rorsapr_sapr_code sap
    from
      rorsapr a
    where
      a.rorsapr_term_code = (select max(b.rorsapr_term_code) from rorsapr b where a.rorsapr_pidm = b.rorsapr_pidm and b.rorsapr_term_code <= '202003')) s
  
  
  --Period Budget Code Lock/FREEZE
  ,(select
      rbrapbg_pidm pidm
      ,RBRAPBG_PBGP_CODE pbcode
      ,decode(RBRAPBG_PBGP_CODE_LOCK_IND, 'Y', 'Yes', 'No') pbcode_locked
      ,decode(RBRAPBG_BUDGET_FREEZE_IND, 'Y', 'Yes', 'No') b_locked
    from
      rbrapbg
    where
      RBRAPBG_RUN_NAME = 'ACTUAL'
      and RBRAPBG_PERIOD = '202003'
      and RBRAPBG_AIDY_CODE = '2021') b
  
  
  
  ,(select
      rorenrl_pidm pidm
      ,rorenrl_term_code term
      ,RORENRL_FINAID_ADJ_HR hr
      ,RORENRL_CONSORTIUM_IND con
    from
      rorenrl
    where
      rorenrl_enrr_code = 'STANDARD'
      and rorenrl_term_code like '202003') c
  ,(select
      sfrstcr_pidm pidm
      ,sfrstcr_term_code term
      ,sum(sfrstcr_credit_hr) hr
    from
      sfrstcr
    where
      sfrstcr_term_code like '202003'
      and sfrstcr_rsts_code in (select stvrsts_code from stvrsts where STVRSTS_INCL_SECT_ENRL = 'Y')
    group by sfrstcr_pidm, sfrstcr_term_code) r
where
  spriden_change_ind is null
  and spriden_pidm = rpratrm_pidm
  and rpratrm_aidy_code = '2021'
  and RPRATRM_AUTHORIZE_AMT is not null
  
  and rpratrm_pidm = c.pidm(+)
  and rpratrm_period = c.term(+)
  and rpratrm_pidm = r.pidm(+)
  and rpratrm_period = r.term(+)
  and rpratrm_period = '202003'
  and rpratrm_pidm = b.pidm(+)
  and rpratrm_pidm = s.pidm(+)
  and rpratrm_pidm = h.pidm(+)
  and rpratrm_pidm = admit.pidm(+)
  and rpratrm_pidm = major.pidm
  
  --and rpratrm_pidm = astd.pidm(+)
  and not exists (select 'asdf' from SGVSTDN a where a.sgvstdn_pidm = rpratrm_period and a.SGVSTDN_term_code = (select max(b.sgvstdn_term_code) from sgvstdn b where b.sgvstdn_term_code <= '202003' and b.sgvstdn_astd_desc is not null and a.sgvstdn_pidm = b.sgvstdn_pidm) and a.sgvstdn_astd_code = 'SU') 
  and (r.hr is not null or c.hr is not null)
  and not exists (select 'afd' from rorstat where rorstat_pidm = rpratrm_pidm and rorstat_aidy_code = rpratrm_aidy_code and RORSTAT_DISB_REQ_COMP_DATE is null)
  and not exists (select 'afd' from rrrareq where rrrareq_pidm = rpratrm_pidm and rrrareq_aidy_code = rpratrm_aidy_code and rrrareq_fund_code = rpratrm_fund_code and RRRAREQ_SAT_IND = 'N')
  --this will exclude those without loans should be a loan only query. if chur is auth but mpn not done, student won't show up
  and exists (select 'asdf' from rlrdlor, rlrdldb where RLRDLOR_PIDM = rlrdldb_pidm and RLRDLOR_PIDM = rpratrm_pidm and RLRDLOR_LOAN_NO = rlrdldb_loan_no and RLRDLOR_AIDY_CODE = rlrdldb_aidy_code
                                                and rlrdlor_aidy_code = rpratrm_aidy_code and RLRDLOR_FUND_CODE = rlrdldb_fund_code and rlrdlor_fund_code = rpratrm_fund_code and RLRDLOR_MPN_LINKED_IND = 'Y')
  

【问题讨论】:

  • 也许我应该把 CTE 变成 from 子句中的子查询...
  • ANSI-92 是采用显式JOIN 语法替换隐式连接(使用, 的标准。那是非常接近30岁。请停止使用来自错误世纪的过时且容易出错的语法。
  • 这是我继承的代码,如果没有必要,我不想重写 1000 行代码。无需我尝试添加此字段,它就可以正常工作。
  • CTE 的主要列。我想我想通了,忘记在 from 子句中添加 CTE。
  • 请关闭问题

标签: sql oracle common-table-expression


【解决方案1】:

将 CTE 添加到 from 子句使其工作。忘记那个小细节了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-16
    • 2023-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-28
    • 2014-10-28
    相关资源
    最近更新 更多