简体   繁体   中英

ORA-01427: single-row subquery returns more than one row- multiple values needed

I have the following SQL query on Oracle

SELECT 
  CASE
    WHEN (SELECT DISTINCT(trim(z.m_base_acct))
      FROM acc_mapping_rep z
      WHERE z.m_dyn_acct=b.m_en_credit
      AND z.M_REF_DATA  =b.M_REF_DATA) IN 'SAMANOS'
    THEN '7200000888001X'

  END AS M_EN_CRDR

FROM 
acc_journal_rep b
AND b.m_ref_data       = 41091
AND b.m_en_date        = '21-SEP-15'
AND b.m_entity         = 'LN'
AND b.m_nb_trn         = 0

I am having the oracle error ORA-01427. Due to the line where I have a case when condition knowing that I did put IN operator instead of equal. Please advise. I need to have multiple values in the case condition.

If as you've shown you only have one value to compare, 'SAMANOS' , then you can just swap the terms:

SELECT 
  CASE
    WHEN 'SAMANOS' IN (SELECT DISTINCT(trim(z.m_base_acct))
      FROM acc_mapping_rep z
      WHERE z.m_dyn_acct=b.m_en_credit
      AND z.M_REF_DATA  =b.M_REF_DATA)
    THEN '7200000888001X'
  END AS M_EN_CRDR
...

You could also left-join to the acc_mapping_rep table, but the distinct and trim are slightly worrying - if those are actually needed then it becomes a little more complicated and you'd need to left-join to an inline view of that table.

The b.m_en_date = '21-SEP-15' is also worrying; if m_en_date is a DATE column then you're relying on implicit conversions and your session NLS settings. It would be better to use to_date() , or with a fixed value a date literal: b.m_en_date = date '2015-09-21' .

Your original longer code would also benefit from using ANSI joins instead of the old Oracle-specific (+) outer join operator.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM