将Access查询转换为SQL Server

时间:2016-02-15 21:52:36

标签: sql sql-server

我正在尝试将此查询转换为SQL Server视图

SELECT 
    IIF([tblStockMovementsDate] ! [LocationType] = 3, [tblShops] ! [Type], [tblLocationType] ! [LocationTypeDesc]) AS [LocationTypeDesc],
    tblStockMovementsDate.[Location],
    qryAllLocations.[WarehouseName],
    SUM (([tblStockMovementsdate] ! [MovementQtyHangers] + ([tblStockMovementsdate] ! [MovementQtyBoxes] * Iif(Isnull([qryGarmentsPerBox] ! [GarmentsPerBox]), 1, [qryGarmentsPerBox] ! [GarmentsPerBox]))) * [qryUnitPrice] ! [UnitPrice]) AS [TotalValue],
    IIF ([tblShops] ! [Type] = 'Concession', 4, [tblStockMovementsDate] ! [LocationType]) AS [LocationType]
FROM   
    (((((tblStockMovementsDate
INNER JOIN 
    tblLocationType ON tblStockMovementsDate.[LocationType] = tblLocationType.[LocationType])
INNER JOIN 
    qryAllLocations ON tblStockMovementsDate.[Location] = qryAllLocations.[WarehouseRef])
LEFT JOIN 
    qryGarmentsPerBox ON (tblStockMovementsDate.[Location] = qryGarmentsPerBox.[WarehouseRef])
                      AND (tblStockMovementsDate.[StockCode] = qryGarmentsPerBox.[StockCode]))
LEFT JOIN 
    qryUnitPrice ON tblStockMovementsDate.[StockCode] = qryUnitPrice.[StockCode])
LEFT JOIN 
    tblShops ON tblStockMovementsDate.[Location] = tblShops.[ShopRef])
INNER JOIN 
    tblStock ON tblStockMovementsDate.[StockCode] = tblStock.[StockCode]
WHERE  
    ((IIF ([tblShops].[Clearance] = 0, [tblStock].[DeadCode] = 0, [tblStock].[RemoveFromClearance] = 0) ) <> False)
GROUP BY 
    IIF ([tblStockMovementsDate] ! [LocationType] = 3, [tblShops] ! [Type], [tblLocationType] ! [LocationTypeDesc]),
    tblStockMovementsDate.[Location], 
    qryAllLocations.[WarehouseName],
    IIF ([tblShops] ! [Type] = 'Concession', 4, [tblStockMovementsDate] ! [LocationType])
ORDER BY
    IIF ([tblShops] ! [Type] = 'Concession', 4, [tblStockMovementsDate] ! [LocationType]) 

但它不会产生输出,而是我不断收到此错误:

  

函数参数列表出错:&#39; =&#39;不承认   函数参数列表出错:&#39; IS&#39;不承认   无法解析查询文本。

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

这是在黑暗中拍摄的。我尽力破译你最初发布的代码。

我建议删除那些嵌套视图。它们会在某些时候引起你严重的痛苦。您可能必须从这些视图中获取查询并将其移至此查询。

请注意,当您使用别名而不是每个列前面的表格的Access默认值时,这是多么清晰。我还删除了几十个没有做任何事情的括号组,但是很难看到。我还将所有IIF表达式转换为case表达式,因为我不知道你使用的是什么版本的sql。

SELECT 
    case when smd.LocationType = 3 
        then tblShops.Type 
        else lt.LocationTypeDesc 
    end AS LocTypeDesc
    , smd.Location
    , al.WarehouseName
    , Sum((smd.MovementQtyHangers + (smd.MovementQtyBoxes * Isnull(g.GarmentsPerBox, 1))) * qryUnitPrice.UnitPrice) AS TotalValue
    , case when sh.Type = 'Concession' 
        then 4 
        else smd.LocationType 
    end AS LocType
FROM tblStockMovementsDate smd
INNER JOIN tblLocationType lt ON smd.LocationType = ll.LocationType
INNER JOIN qryAllLocations al ON smd.Location = al.WarehouseRef
LEFT JOIN qryGarmentsPerBox g ON smd.Location = g.WarehouseRef 
                                AND smd.StockCode = g.StockCode
LEFT JOIN qryUnitPrice up ON smd.StockCode = up.StockCode
LEFT JOIN tblShops sh ON smd.Location = sh.ShopRef
INNER JOIN tblStock st ON smd.StockCode = st.StockCode
WHERE tblShops.Clearance <> 0
    OR tblStock.DeadCode <> 0
    OR tblStock.RemoveFromClearance <> 0 
GROUP  BY case when smd.LocationType = 3 then tblShops.Type else lt.LocationTypeDesc end
    , smd.Location
    , al.WarehouseName
    , case when sh.Type = 'Concession' then 4 else smd.LocationType end
order by LocType