简体   繁体   English

SQL IsNull与查询中的SELECT子查询

[英]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. 这条SQL是大量文字的一部分,无需进行IsNULL检查即可使用。 With IsNUll it outputs the error bellow. 使用IsNUll,它会输出以下错误消息。 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. 我在该特定列上没有null,并且可以从另一个表中获得正确的结果。 I don't know how to do it. 我不知道该怎么做。

Subquery returned more than 1 value. 子查询返回的值超过1。 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] . 该消息很简单:在表[SC Vermorel SRL$Production Order]每个[Old Prod_ Order No_][Source No_]可以找到多个[Replan Ref_ No_] [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 确保您的表不包含重复项(具有唯一约束),或者将子查询更改为仅返回一个值,例如,replace

SELECT DISTINCT PO.[Replan Ref_ No_]

with

SELECT MIN(PO.[Replan Ref_ No_])

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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