简体   繁体   中英

SQL IsNull with SELECT subquery in a query

I have this:

ISNULL(FPONO.[Replan Ref_ No_],(SELECT DISTINCT PO.[Replan Ref_ No_]
FROM NAV_Vermorel_Live.dbo.[SC Vermorel SRL$Production Order] AS PO
WHERE SH.[External Document No_] = PO.[Old Prod_ Order No_] AND PO.[Source No_] = SL.No_))

This piece of SQL is part of big wall of text, which works without the IsNULL check. With IsNUll it outputs the error bellow. Could anyone point me in a right direction? I have null on that specific column, and I can get the right results from another table. I don't know how to do it.

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

SELECT SL.[Document No_], 
SL.[Sell-to Customer No_], 
SL.Type,
SL.[Line No_], 
ISNULL(FPONO.[Replan Ref_ No_],(SELECT DISTINCT PO.[Replan Ref_ No_]
                                FROM NAV_Vermorel_Live.dbo.[SC Vermorel SRL$Production Order] AS PO
                                WHERE SH.[External Document No_] = PO.[Old Prod_ Order No_] AND PO.[Source No_] = SL.No_)),
SL.No_, 
SL.[Location Code], 
SL.[Posting Group], 
SL.[Shipment Date], 
SL.Description, 
SL.[Unit of Measure], 
SL.Quantity, 
SL.[Outstanding Quantity], 
SL.[Qty_ to Invoice], 
SL.[Qty_ to Ship], 
SL.[Unit Price], 
SL.Amount,
SL.[Net Weight],
SL.[Outstanding Amount], 
SL.[Qty_ Shipped Not Invoiced], 
SL.[Quantity Shipped], 
SL.[Quantity Invoiced], 
SL.[Gen_ Prod_ Posting Group], 
SL.[Line Amount], 
SL.[Item Category Code], 
SL.[Requested Delivery Date],  
SL.[Shipping Time], 
SL.[Piece Index], 
SL.Urgenta, 
SL.[Document Type], 
SH.[External Document No_], 
Cust.Name
FROM NAV_Vermorel_Live.dbo.[SC Vermorel SRL$Sales Line] AS SL 
INNER JOIN NAV_Vermorel_Live.dbo.[SC Vermorel SRL$Sales Header] AS SH ON SL.[Document No_] = SH.No_ 
INNER JOIN NAV_Vermorel_Live.dbo.[SC Vermorel SRL$Customer] AS Cust ON SL.[Sell-to Customer No_] = Cust.No_ 
left JOIN (SELECT 
            RE1."Entry No_", 
            RE1."Item No_", 
            RE1."Quantity (Base)", 
            RE1."Source Subtype", 
            RE1."Source ID",  
            RE1."Source Type",
            RE1."Source Ref_ No_",
            RE2."Source Ref_ No_" AS SRN,
            RE2."Source ID" As SalesOrder,
            PO.[Replan Ref_ No_]
            FROM NAV_Vermorel_Live.dbo."SC Vermorel SRL$Reservation Entry" AS RE1 JOIN NAV_Vermorel_Live.dbo."SC Vermorel SRL$Production Order" AS PO
            ON RE1."Source ID" = PO.No_ 
            right JOIN (SELECT 
                        RE."Entry No_", 
                        RE."Item No_", 
                        RE."Quantity (Base)", 
                        RE."Source Subtype", 
                        RE."Source ID", 
                        RE."Source Ref_ No_" 
                        FROM NAV_Vermorel_Live.dbo."SC Vermorel SRL$Reservation Entry" AS RE
                        WHERE RE."Source Subtype"=1 AND RE.[Source Type] = 37) AS RE2 ON RE1.[Entry No_] = RE2.[Entry No_]
            WHERE (RE1."Source Type" = 5406 OR RE1."Source Type" = 5407) AND (RE1."Source Subtype" = 2 OR RE1."Source Subtype" = 3)) AS FPONO 
    ON (SL.[Document No_] = FPONO.[SalesOrder]) 
        AND (FPONO."SRN" = SL.[Line No_]) 
        AND (FPONO.[Item No_] = SL.[No_])
WHERE  (SL.[Outstanding Quantity] > 0) 
    AND (SL.[Location Code] = 'MACH FIN' 
        OR SL.[Location Code] = 'MAGAZIN NC' 
        OR SL.[Location Code] = 'MARFURI') 

The message is simple: it is possible to find more than one [Replan Ref_ No_] per [Old Prod_ Order No_] and [Source No_] in your table [SC Vermorel SRL$Production Order] .

Either make sure your table doesn't contain duplicates (with a unique constraint) or change the subquery to return one value only, eg replace

SELECT DISTINCT PO.[Replan Ref_ No_]

with

SELECT MIN(PO.[Replan Ref_ No_])

您还应该能够在子查询中使用前1名。

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