简体   繁体   中英

Not permitted when the subquery follows =, !=, <, <= , >, >= or when used as an expression

SELECT 
    a.APOIT AS 'Item', 
    (SELECT SUBSTRING(LTRIM(RTRIM(Requisition)), 1, 6) as Requisition 
     FROM DBO_POLB015.db_datareader.CMSFIL_POLB15 as b 
     WHERE b.PONO = a.APONO and b.Item = a.APOIT) as 'Req', 
    REPLACE(REPLACE(REPLACE(REPLACE(ltrim(rtrim(a.AD30A)) + 
        CASE 
           WHEN STUFF((SELECT '; ' + (LTRIM(RTRIM(AI50A)) + LTRIM(RTRIM(AI50B)) + LTRIM(RTRIM(AI50C)) ) 
                       FROM [DBO_CMSFIL].[dbo].[POTMDI] 
                       LEFT OUTER JOIN [DBO_CMSFIL].[dbo].POTMDT ON POTMDI.APOIT = POTMDT.APOIT AND POTMDI.ADVNO = POTMDT.ADVNO AND POTMDI.ACONO = POTMDT.ACONO AND POTMDI.APONO = POTMDT.APONO 
                       WHERE POTMDI.APONO = a.APONO and POTMDT.APOIT= a.APOIT 
                       FOR XML PATH('')), 1, 1, '') IS NULL
              THEN ''  
              ELSE STUFF((SELECT '; ' + (ltrim(rtrim(AI50A)) + ltrim(rtrim(AI50B)) + ltrim(rtrim(AI50C))) 
                          FROM [DBO_CMSFIL].[dbo].[POTMDI] 
                          LEFT OUTER JOIN [DBO_CMSFIL].[dbo].POTMDT ON POTMDI.APOIT = POTMDT.APOIT AND POTMDI.ADVNO = POTMDT.ADVNO AND POTMDI.ACONO = POTMDT.ACONO AND POTMDI.APONO = POTMDT.APONO 
                          WHERE POTMDI.APONO = a.APONO and POTMDT.APOIT= a.APOIT 
                          FOR XML PATH('')), 1, 1, '') 
             end,
    '&amp;',' & '),'  ',''),'&#39;', '\'),'&quot;', '\"') as Description,  
    a.AQTOR AS 'QtyOrdered', 
    A.AUM AS 'Units' 
FROM
    [DBO_CMSFIL].[dbo].[POTMDT] AS a 
WHERE   
    a.APONO = '152988'
ORDER BY 
    a.APOIT

I get the following message:

Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

How can I fix this?

Exactly as the error message says: You have a subquery that's returning multiple columns and/or multiple rows into a context where only a SINGLE value+row is allowed.

If it's multiple rows, you can trivially fix by switching to an IN match. If it's multiple fields, that's just flat-out wrong.

SELECT (SELECT * FROM tablewithlotsoforws)  // wrong
SELECT (SELECT singlefield FROM tablewithlotsofrows WHERE match=singlerow) // ok
SELECT ... WHERE foo = (SELECT lotsofrows FROM sometable) // wrong
SELECT ... WHERE foo IN (SELECT lotsofrows FROM sometable) // ok
SELECT ... WHERE foo IN (SELECT lotsofrows, evenmorerows FROM stable) // wrong again

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM