子查询返回多行

时间:2014-03-11 16:25:51

标签: sql oracle plsql

我遇到以下SQL语句的问题。我收到一个错误,说“ORA-01427:单行子查询返回多行”,我不确定如何修复错误。任何帮助是极大的赞赏!谢谢布拉德

select t.check_amount,
   t.check_amount_text,
   t.agency_no,
   t.branch_no,
   substr(t.payee_name,1,10),
   substr(t.payee_name,11),
   (select c.contact_primary_email 
    from contact c,
         contact_role cr,
         branch b
    where c.contact_id = cr.contact_id
          and cr.entity_id = b.branch_id
          and cr.contact_role_code = 'ACHCONTACT')

from CHECK_REGISTER t
where t.check_type = 'C' 
  and t.check_date > to_date('01/31/2014','mm/dd/yyyy')
and t.disbursement_fund_type = 'ACH'

1 个答案:

答案 0 :(得分:3)

发生错误是因为您有select语句,其中包含单个值。 select返回多行,因此Oracle报告错误。

在诸如你的查询中,这通常是因为您需要将内部子查询关联到外部查询。我最好的猜测是,此查询需要分支机构主要联系人的电子邮件地址。为此,查询将是:

   (select c.contact_primary_email 
    from contact c join
         contact_role cr
         on c.contact_id = cr.contact_id join
         branch b
         on cr.entity_id = b.branch_id
    where b.branch_no = t.branch_no and cr.contact_role_code = 'ACHCONTACT'
   ) as email

这假设branch_no上的相关条件。

相关问题