从表中选择数据时我得到了错误

问题描述 投票:0回答:1

SQL:

select E.ID
    , cp = (
        select ISNULL(C.CNAME, E.CP) CP
        from TBL_EXPEDITE_MASTER E
        left join [NEPTUNE122].BB_PROVISION.DBO.BI_CUSTOMERS C on C.spid = E.CP
        )
    , E.ECO_ORDER
    , E.DN
    , E.CBUK
    , E.XMPF_ID
    , E.CP
    , E.RECEIVED
    , E.CP_OWNER
    , E.CP_CONTACT_NUMBER
    , CP_EMAIL
    , E.CPREF
    , E.SITE_CONTACT_NAME
    , E.SITE_CONTACT_NUMBER
    , E.EXISTING_CRD
    , E.NEW_CRD
    , E.EXPEDITE_CRD
    , E.status
    , E.LINE
    , E.CHARGEABLE
    , E.RESPONSE
    , LAST_UPDATED
    , ORDER_TYPE
    , INITIALRESP
    , NOTES
    , BTW_EXPEDITE_REF --1.2
    , ProductType --1.2
    , InstallationAddress --1.2
    , SITE_CONTACT_OTHER --1.2
    , FastTrackReason --1.2
    , FastTrackFOC --1.2
    , FOC_Reason --1.2
    , CP_EMAIL_OTHER --1.2
    , Received_Via --1.2
    , E2EData --1.2
    , E.organisationName
    , E.postCode
    , E.thoroughfareDescriptor
    , E.county
    , E.country
from TBL_EXPEDITE_MASTER E with (nolock)

错误信息

enter image description here

1

sql-server
1个回答
1
投票

您收到此错误是因为在First Select您正在使用的SubQuery为同一行返回了多于1个Record。

您可以尝试使用top 1来避免此错误或任何聚合函数或将子查询作为单独的CTE加入也可以正常工作。

对于快速修复,您可以像这样更改查询

SELECT cp =
(
    SELECT TOP 1 ISNULL(C.CNAME, E.CP) CP
    FROM TBL_EXPEDITE_MASTER E
         LEFT JOIN [NEPTUNE122].BB_PROVISION.DBO.BI_CUSTOMERS C ON C.spid = E.CP 
ORDER BY C.CNAME, E.CP
);

但是看看你的查询,我认为这也会有用

SELECT 
    E.ID,
    cp = ISNULL(C.CNAME, E.CP),
    E.ECO_ORDER,
    E.DN,
    E.CBUK,
    E.XMPF_ID,
    E.CP,
    E.RECEIVED,
    E.CP_OWNER,
    E.CP_CONTACT_NUMBER,
    CP_EMAIL,
    E.CPREF,
    E.SITE_CONTACT_NAME,
    E.SITE_CONTACT_NUMBER,
    E.EXISTING_CRD,
    E.NEW_CRD,
    E.EXPEDITE_CRD,
    E.STATUS,
    E.LINE,
    E.CHARGEABLE,
    E.RESPONSE,
    LAST_UPDATED,
    ORDER_TYPE,
    INITIALRESP,
    NOTES,
    BTW_EXPEDITE_REF,
    ProductType,
    InstallationAddress,
    SITE_CONTACT_OTHER,
    FastTrackReason,
    FastTrackFOC,
    FOC_Reason,
    CP_EMAIL_OTHER,
    Received_Via,
    E2EData,
    E.organisationName,
    E.postCode,
    E.thoroughfareDescriptor,
    E.county,
    E.country
    FROM TBL_EXPEDITE_MASTER E WITH (NOLOCK)
       LEFT JOIN [NEPTUNE122].BB_PROVISION.DBO.BI_CUSTOMERS C 
          ON C.spid = E.CP
© www.soinside.com 2019 - 2024. All rights reserved.