创建 SQL 查询来提取采购订单信息时,检索与采购订单相关的商品编号的子查询返回以下错误:ORA-01427:单行子查询返回多于一行。
以下是完整的 SQL 查询:
SELECT
distinct item.item_number item_number
FROM
po_headers_all pha
,fun_all_business_units_v fav
,po_buyers_val_v pbv_inner
,poz_suppliers_v psv
,po_lines_all pla
,egp_categories_vl egp
,hr_locations_all hla
,hr_locations_all hla2
,po_distributions_all pda
,po_line_locations_all plla
,gl_code_combinations gcc
,pjf_tasks_v pt
,poz_supplier_sites_all_m pss
,hr_organization_units hru
,(select pjf.name,pb.segment1,pb.project_id
from pjf_projects_all_b pb, pjf_projects_all_tl pjf
where pjf.project_id = pb.project_id)
project
,(select pha2.segment1 ,pla2.line_num, pla2.po_line_id,pha2.po_header_id
from po_headers_all pha2 ,po_lines_all pla2
where pha2.po_header_id = pla2.po_header_id
and pha2.type_lookup_code(+) = 'BLANKET')
agreement
,(select esiav.item_number,esiav.inventory_item_id
FROM egp_system_items_b esiav,inv_org_parameters IOP
WHERE ESIAV.organization_id = IOP.organization_id
AND IOP.organization_code='WSH_ITEMMSTR')
item
/* Old logic- requisition number not getting populated
,(select
prh.requisition_number
,prl.po_header_id
,prl.req_bu_id
,prl.po_line_id
,pf.full_name
from
por_requisition_lines_all prl
,por_requisition_headers_all prh
,PER_PERSON_NAMES_F pf
where
prl.requisition_header_id = prh.requisition_header_id
AND prl.requester_id= pf.person_id(+)
AND pf.name_type='GLOBAL')
rq */
WHERE 1=1
and pha.po_header_id = pla.po_header_id
and pha.po_header_id = pda.po_header_id (+)
and pha.agent_id = pbv_inner.person_id
and pha.vendor_id = psv.vendor_id
and plla.ship_to_location_id = hla.location_id(+)
and pha.req_bu_id = fav.bu_id(+)
and pha.vendor_site_id = pss.vendor_site_id
and pla.po_line_id = pda.po_line_id(+)
and pla.item_id = item.inventory_item_id(+)
and pla.category_id = egp.category_id
and pla.po_line_id = plla.po_line_id
and pla.from_header_id = agreement.po_header_id(+)
and pla.from_line_id = agreement.po_line_id(+)
and pda.deliver_to_location_id = hla2.location_id(+)
and pda.code_combination_id = gcc.code_combination_id(+)
and pda.pjc_organization_id = hru.organization_id(+)
and pda.pjc_project_id = project.project_id(+)
and pda.pjc_task_id = pt.task_id(+)
and pss.vendor_id = psv.vendor_id
/* Old logic- requisition number not getting populated
and pha.po_header_id = rq.po_header_id(+)
and pla.req_bu_id = rq.req_bu_id(+)
and pla.po_line_id = rq.po_line_id(+) */
and pha.type_lookup_code = 'STANDARD'
and pha.document_status not in ('INCOMPLETE','CANCELED','REJECTED')
and pla.line_status not in ('INCOMPLETE','CANCELLED')
我希望能够检索与采购订单相关的所有项目编号,并进行最小的更改。
如果我正确获取了查询的联合条件,那么使用 join sintax 的代码应如下所示:
-- NOT CHECKED NOR TESTED IN ANY WAY ***
SELECT Distinct item.item_number as item_number
FROM po_headers_all pha
INNER JOIN po_buyers_val_v pbv_inner ON( pha.agent_id = pbv_inner.person_id )
INNER JOIN po_lines_all pla ON( pha.po_header_id = pla.po_header_id )
INNER JOIN po_line_locations_all plla ON( pla.po_line_id = plla.po_line_id )
INNER JOIN poz_supplier_sites_all_m pss ON( pha.vendor_site_id = pss.vendor_site_id )
INNER JOIN poz_suppliers_v psv ON( pha.vendor_id = psv.vendor_id And
pss.vendor_id = psv.vendor_id )
INNER JOIN egp_categories_vl egp ON( pla.category_id = egp.category_id )
--
LEFT JOIN fun_all_business_units_v fav ON( pha.req_bu_id = fav.bu_id )
LEFT JOIN po_distributions_all pda ON( pha.po_header_id = pda.po_header_id And
pla.po_line_id = pda.po_line_id )
LEFT JOIN hr_locations_all hla ON( plla.ship_to_location_id = hla.location_id )
LEFT JOIN hr_locations_all hla2 ON( pda.deliver_to_location_id = hla2.location_id )
LEFT JOIN gl_code_combinations gcc ON( pda.code_combination_id = gcc.code_combination_id )
LEFT JOIN pjf_tasks_v pt ON( pda.pjc_task_id = pt.task_id )
LEFT JOIN hr_organization_units hru ON( pda.pjc_organization_id = hru.organization_id )
--
LEFT JOIN ( Select pjf.name,pb.segment1, pb.project_id
From pjf_projects_all_b pb, pjf_projects_all_tl pjf
Where pjf.project_id = pb.project_id
) project ON( pda.pjc_project_id = project.project_id )
--
LEFT JOIN ( Select pha2.segment1, pla2.line_num, pla2.po_line_id, pha2.po_header_id
From po_headers_all pha2
Left Join po_lines_all pla2 ON( pha2.po_header_id = pla2.po_header_id And
pha2.type_lookup_code = 'BLANKET' )
) agreement ON( pla.from_header_id = agreement.po_header_id And
pla.from_line_id = agreement.po_line_id )
--
LEFT JOIN ( Select esiav.item_number, esiav.inventory_item_id
From egp_system_items_b esiav
Inner Join inv_org_parameters iop ON( esiav.organization_id = IOP.organization_id And
IOP.organization_code = 'WSH_ITEMMSTR' )
) item ON( pla.item_id = item.inventory_item_id )
--
WHERE pha.type_lookup_code = 'STANDARD' And
pha.document_status not in ('INCOMPLETE','CANCELED','REJECTED') And
pla.line_status not in ('INCOMPLETE','CANCELLED')
您似乎正在使用它来过滤另一个查询 - 采购订单信息。如果在该查询中您有类似以下内容:
...
purchase_order.item_number = (the above query)
...
然后你会从这个问题的标题中得到错误消息。如果查询返回多于 1 行,则会出现该错误。我们无法判断情况是否如此,因为没有样本数据,没有预期结果,而且我们对采购订单查询一无所知。
您可以尝试更改采购订单查询中的条件以使用 IN 而不是 = 运算符
...
purchase_order.item_number IN(the above query)
...
这应该消除有问题的错误,但这当然并不意味着结果是好的。您应该检查您的实际数据和代码,看看是否存在需要 1 个且仅 1 个值的条件。该查询几乎肯定会返回超过 1 行。