SQL嵌套IIF函数

问题描述 投票:0回答:2

我尝试使用嵌套的IIF函数在SQL中创建一个复杂的公式。此公式中有许多IFF。但是以某种方式,Microsoft查询不会采用以下语句:enter image description here

SELECT

IIF (system_Machine.Machine_omschrijving IN ('BE'),
IIF (PD_FleeceRecipe.FleeceRecipe_IntermediateLayer1Width is NULL,
IIF (PD_FleeceRecipe.FleeceRecipe_BottomLayerWidth is NULL, PD_FleeceRecipe.FleeceRecipe_IntermediateLayer2Width, PD_FleeceRecipe.FleeceRecipe_BottomLayerWidth)
,
IIF(PD_FleeceRecipe.FleeceRecipe_BottomLayerWidth is NULL,
IIF(PD_FleeceRecipe.FleeceRecipe_IntermediateLayer1Width is NULL, PD_FleeceRecipe.FleeceRecipe_BottomLayerWidth, PD_FleeceRecipe.FleeceRecipe_IntermediateLayer1Width)
,
PD_FleeceRecipe.FleeceRecipe_BottomLayerWidth-PD_FleeceRecipe.FleeceRecipe_IntermediateLayer2Width+PD_FleeceRecipe.FleeceRecipe_IntermediateLayer1Width-PD_FleeceRecipe.FleeceRecipe_IntermediateLayer2Width)
)))
[res] ,

PD_Packaging.Packaging_Itemnr,  
PD_Packaging.Packaging_Description,  
PD_Packaging.Packaging_Width,   
PD_Packaging.Packaging_Weight,   
PD_Packaging.Packaging_Weightgm2,   
PD_Packaging.Packaging_Overlap,   
PD_Packaging.Packaging_Verstrekking,   
PD_Packaging.Packaging_CategoryName,    
PD_Main.Main_StatusID,  
PD_Main.Main_Itemnr,
system_Machine.Machine_omschrijving,   
PD_Main.Main_Product,   
PD_Main.Main_WidthTobeInvoiced,  
PD_Main.Main_AssemblingRollDiameter,  
PD_Main.Main_AssemblingRollLength,  
PD_Main.Main_LabelArea,    
PD_FleeceRecipe.FleeceRecipe_IntermediateLayer1Width,  
PD_FleeceRecipe.FleeceRecipe_IntermediateLayer2Width,  
PD_FleeceRecipe.FleeceRecipe_BottomLayerWidth

FROM testsystemOBB.dbo.PD_Packaging PD_Packaging    
LEFT OUTER JOIN testsystemOBB.dbo.PD_Main
ON PD_Packaging.Packaging_ID = PD_Main.Main_AssemblingPackagingSingleRollID AND PD_Main.Main_StatusID = 2

LEFT OUTER JOIN testsystemOBB.dbo.PD_FleeceRecipe  
ON PD_Main.Main_ID = PD_FleeceRecipe.FleeceRecipe_MainID AND FleeceRecipe_Preferred = 1    

LEFT OUTER JOIN testsystemOBB.dbo.BOM_Results  
ON PD_Main.Main_Itemnr = BOM_Results.Item_Number

LEFT OUTER JOIN testsystemOBB.dbo.system_Machine  
ON BOM_Results.SelMachineID = system_Machine.Machine_id

WHERE (PD_Packaging.Packaging_CategoryName='STRETCH')  AND (PD_Main.Main_Itemnr = 406181)

[我还尝试了一个带有另一个ISNULL函数的小例子,但是结果是“选择列列表后面有意外的RES。我不明白我的错误是什么。

SELECT

IIF (system_Machine.Machine_omschrijving IN ('BE'),
IIF (ISNULL(FleeceRecipe_IntermediateLayer1Width), 3,4))

[res] ,

PD_Packaging.Packaging_Itemnr,  
PD_Packaging.Packaging_Description,  
PD_Packaging.Packaging_Width,   
PD_Packaging.Packaging_Weight,   
PD_Packaging.Packaging_Weightgm2,   
PD_Packaging.Packaging_Overlap,   
PD_Packaging.Packaging_Verstrekking,   
PD_Packaging.Packaging_CategoryName,    
PD_Main.Main_StatusID,  
PD_Main.Main_Itemnr,
system_Machine.Machine_omschrijving,   
PD_Main.Main_Product,   
PD_Main.Main_WidthTobeInvoiced,  
PD_Main.Main_AssemblingRollDiameter,  
PD_Main.Main_AssemblingRollLength,  
PD_Main.Main_LabelArea,    
PD_FleeceRecipe.FleeceRecipe_IntermediateLayer1Width,  
PD_FleeceRecipe.FleeceRecipe_IntermediateLayer2Width,  
PD_FleeceRecipe.FleeceRecipe_BottomLayerWidth

FROM testsystemOBB.dbo.PD_Packaging PD_Packaging    
LEFT OUTER JOIN testsystemOBB.dbo.PD_Main
ON PD_Packaging.Packaging_ID = PD_Main.Main_AssemblingPackagingSingleRollID AND PD_Main.Main_StatusID = 2

LEFT OUTER JOIN testsystemOBB.dbo.PD_FleeceRecipe  
ON PD_Main.Main_ID = PD_FleeceRecipe.FleeceRecipe_MainID AND FleeceRecipe_Preferred = 1    

LEFT OUTER JOIN testsystemOBB.dbo.BOM_Results  
ON PD_Main.Main_Itemnr = BOM_Results.Item_Number

LEFT OUTER JOIN testsystemOBB.dbo.system_Machine  
ON BOM_Results.SelMachineID = system_Machine.Machine_id

WHERE (PD_Packaging.Packaging_CategoryName='STRETCH')  AND (PD_Main.Main_Itemnr = 406181)

enter image description here

sql sql-server tsql if-statement isnull
2个回答
0
投票

您有语法错误。IIF功能包含三个必需部分:条件表达式,真值和假值。

[您在两个查询中都遗漏了最外层的IIF函数中的错误表达式,之后还缺少了一个逗号(在第一个查询中,有太多的右括号)]]

在您的第一个查询中:

iif

第二个查询:

SELECT

IIF (
    system_Machine.Machine_omschrijving IN ('BE'),
    IIF(PD_FleeceRecipe.FleeceRecipe_IntermediateLayer1Width is NULL,
        IIF (PD_FleeceRecipe.FleeceRecipe_BottomLayerWidth is NULL, 
             PD_FleeceRecipe.FleeceRecipe_IntermediateLayer2Width, 
             PD_FleeceRecipe.FleeceRecipe_BottomLayerWidth
        ),
        IIF(PD_FleeceRecipe.FleeceRecipe_BottomLayerWidth is NULL,
            IIF(PD_FleeceRecipe.FleeceRecipe_IntermediateLayer1Width is NULL, 
                PD_FleeceRecipe.FleeceRecipe_BottomLayerWidth, 
                PD_FleeceRecipe.FleeceRecipe_IntermediateLayer1Width
            ),
            PD_FleeceRecipe.FleeceRecipe_BottomLayerWidth - PD_FleeceRecipe.FleeceRecipe_IntermediateLayer2Width + PD_FleeceRecipe.FleeceRecipe_IntermediateLayer1Width - PD_FleeceRecipe.FleeceRecipe_IntermediateLayer2Width
        )
    )
/* Missing a false value here...*/) 
-- ) this closing parenthesis shouldn't be here. Instead, there should be a comma.
[res] ,

0
投票

尝试改用嵌套的case语句。

© www.soinside.com 2019 - 2024. All rights reserved.