2013-02-28 123 views
0

在case語句中執行此選擇時收到上述錯誤。我嘗試了幾種方法,但無法獲得正確的語法。ORA-00936:缺少表達式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

您最好解釋一下您想實現的目標。您當前的查詢沒有多大意義 – zerkms 2013-02-28 21:06:24

+1

同意。以爲我知道你需要什麼,直到我試圖編輯它並修復它,然後意識到....有些東西丟失了。在你的情況下選擇是一個問題;就我所知,您無法從案例中返回多列。 – 2013-02-28 21:09:35

+1

更多我看看它,爲什麼不完全跳過案例,並將borrower_org_id返回到下一列中的一列中,lender_org_id?看來你的佈局在當前狀態下非常令人困惑。 – 2013-02-28 21:12:15

回答

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

+0

謝謝Cha !!!!完美的作品。我看到了一個例子,其結尾爲,但沒有意識到你在那裏使用了別名。 – user2121274 2013-03-01 14:55:14