获得以下错误ORA-01427:单行子查询返回多行01427. 00000 - “单行子查询返回多行”

时间:2017-12-15 22:09:32

标签: sql oracle

我收到以下错误:

我想从第一个查询中获取列值,并将该值传递给下一个查询。

ORA-01427:单行子查询返回多行 01427. 00000 - “单行子查询返回多行”

使用以下查询:

 with

deal_XX AS(

SELECT distinct idh.vendor supplier
                  ,idh.deal_id
                  ,mff_report.mff_merch_sql.get_sup_name(idh.vendor) sup_name


              FROM im_doc_head idh
                  ,mff_report.stage_complex_deal_head_hist scdhh
                  ,im_complex_deal_detail icdd
                  ,mff_report.v_loc vl
                  ,item_master im
                                  ,item_master im_parent
             WHERE (   (    idh.type IN ('DEBMEC','CRDMEC') --Debit and Credit Memos in APPROVED or POSTED
                        AND idh.status IN ('APPRVE','POSTED'))
                    OR (    idh.type = 'CRDNRC'             --Credit Note Requests in APPROVED or MATCHED
                        AND idh.status IN ('APPRVE','MTCH')))
               AND idh.deal_type = 'C'
               AND NVL(:PM_supplier,idh.vendor) = idh.vendor

               AND idh.deal_id = scdhh.deal_id (+)
               AND SUBSTR(idh.ext_doc_id,(INSTR(idh.ext_doc_id,'-',1) + 1),INSTR(idh.ext_doc_id,'-',1,2) - (INSTR(idh.ext_doc_id,'-',1) + 1)) = scdhh.deal_detail_id (+)
               AND idh.doc_date = scdhh.end_invoice_date (+)
               AND idh.doc_id = icdd.doc_id
               AND icdd.location = vl.loc
               AND icdd.item = im.item
                               AND im.item_parent = im_parent.item (+)
               AND (   :PM_supplier IS NOT NULL
                    OR :PM_doc_date_from IS NOT NULL
                    OR :PM_doc_date_to IS NOT NULL
                    OR :PM_approval_date_from IS NOT NULL
                    OR :PM_approval_date_to IS NOT NULL
                    OR :PM_ext_doc_id IS NOT NULL
                    OR :PM_batch_mode = 'Y')
                    )  

  select distinct  ship.order_no    , (select deal_id from deal_XX) hhhh
  from ordloc_discount od
     ,shipment ship
     ,mff_report.stage_complex_deal_head_hist scdhh  
    where od.deal_ID = (select deal_id from deal_XX) 

    and od.deal_id = scdhh.deal_id 
    and ship.status_code = 'R'
    and od.order_no = ship.order_no
    and ship.receive_date BETWEEN
        to_date(scdhh.start_invoice_date , 'YYYY-MM-DD" "HH24:MI:SS')
         AND
         to_date(scdhh.end_invoice_date , 'YYYY-MM-DD" "HH24:MI:SS') ;

1 个答案:

答案 0 :(得分:0)

如果您运行Q1:select deal_id from deal_XX,那么您可能会获得多行。因此,有两个问题:

  1. 拥有select *, (subselect) from tableX后,子选择必须始终为tableX的每一行返回一个值。由于您将Q1作为子选择,因此查询失败。

  2. 同样的问题是select * from tableX X where X.y = (subselect)。同样,查询处理器希望您提供一个子选择,为TableX的每一行返回一个值,并且Q1返回多行。

  3. 解决方案取决于您要实现的目标。第二个问题可以使用od.deal_ID IN (select deal_id from deal_XX)修复,这意味着您需要在deal_id返回的deal_XX列表中找到可以找到deal_id的行。