ORA-00936:缺少表达式case语句

时间:2013-02-28 21:03:10

标签: oracle select case

使用case语句执行此选择时收到上述错误。我尝试了几种方法,但无法使语法正确。

SELECT m.md_security_id,
   thv.unit_qty,
   thv.contract_value_amt,
   thv.contract_currency_cd,
   (case when
   THV.BORROW_LOAN_TYPE_CD = 'B'
   then select THV.SENDER_ORG_ID as BORROWER_ORG_ID,
           select THV.RECIPIENT_ORG_ID as LENDER_ORG_ID
     end),
   (case when
   THV.BORROW_LOAN_TYPE_CD = 'L'
   THEN select THV.RECIPIENT_ORG_ID as BORROWER_ORG_ID,
            select THV.SENDER_ORG_ID as LENDER_ORG_ID
    end)   
    FROM eqmddev.txn_header_mv thv,
   eqmddev.md_security m,
   eqmddev.security_xref_vw_tbl x
   WHERE     m.isin = x.security_id
   AND ThV.SECURITY_MASTER_ID = X.SECURITY_MASTER_ID
   AND M.EXCHANGE_CD = X.EXCHANGE_CD

1 个答案:

答案 0 :(得分:1)

你需要像这样重写你的查询:

SELECT m.md_security_id,
   thv.unit_qty,
   thv.contract_value_amt,
   thv.contract_currency_cd,
   case 
    when THV.BORROW_LOAN_TYPE_CD = 'B' then THV.SENDER_ORG_ID 
    when THV.BORROW_LOAN_TYPE_CD = 'L' THEN THV.RECIPIENT_ORG_ID 
   end AS BORROWER_ORG_ID,
   case 
    when THV.BORROW_LOAN_TYPE_CD = 'B' then THV.RECIPIENT_ORG_ID 
    when THV.BORROW_LOAN_TYPE_CD = 'L' THEN THV.SENDER_ORG_ID 
   end AS LENDER_ORG_ID,
FROM eqmddev.txn_header_mv thv,
   eqmddev.md_security m,
   eqmddev.security_xref_vw_tbl x
WHERE     m.isin = x.security_id
   AND ThV.SECURITY_MASTER_ID = X.SECURITY_MASTER_ID
   AND M.EXCHANGE_CD = X.EXCHANGE_CD

基本上,您不能从CASE语句返回多个列。将其视为单个列的条件替换。在上面的示例中,我对BORROWER_ORG_IDLENDER_ORG_ID别名使用了相同的CASE条件。

在旁注中,您还应考虑在您的CASE中加入ELSE语句。否则,如果THV.BORROW_LOAN_TYPE_CD不是BL,您将获得NULL