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)
错误消息
1:
答案 0 :(得分:1)
您收到此错误,因为在第一次选择您正在使用的SubQuery已为同一行返回了多于1条记录。
您可以尝试使用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