SQL IsNull在查询中带有SELECT子查询

时间:2016-11-17 13:45:07

标签: sql sql-server select subquery

我有这个:

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_))

这段SQL是文本大墙的一部分,没有IsNULL检查。使用IsNUll,它会输出错误。有人能指出我正确的方向吗?我在该特定列上有null,我可以从另一个表中获得正确的结果。我不知道怎么做。

子查询返回的值超过1。当子查询遵循=,!=,<,< =,>,> =或子查询用作表达式时,不允许这样做。

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') 

2 个答案:

答案 0 :(得分:1)

信息很简单:您可以在表[Replan Ref_ No_]中找到每[Old Prod_ Order No_][Source No_] [SC Vermorel SRL$Production Order]个。{/ p>

确保您的表不包含重复项(具有唯一约束)或更改子查询以仅返回一个值,例如取代

SELECT DISTINCT PO.[Replan Ref_ No_]

SELECT MIN(PO.[Replan Ref_ No_])

答案 1 :(得分:0)

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