2012-02-23 111 views
-1

我查询不运行,但不返回任何结果:的MS Access/SQL Server的传递查询

SET NoCount ON 

SELECT 
    Inventory.EffectiveDate, 
    Inventory.Quantity, 
    Inventory.SourceType, 
    Inventory.PickingLocation, 
    Inventory.SourceInventory, 
    Locations.LocationId, 
    Customers.CustomerName, 
    Products.ProductId, 
    LocationFrom.LocationId as lFrom, 
    LocationTo.LocationId as lTo 

FROM (((((((dbo.Inventory AS Inventory 

    LEFT JOIN dbo.Products AS Products ON Products.Product = Inventory.Product) 
    LEFT JOIN dbo.Locations AS Locations ON Locations.Location = Inventory.Location) 
    LEFT JOIN dbo.Customers AS Customers ON Customers.ConsignmentLocation = Inventory.Location) 

    LEFT JOIN dbo.Inventory AS SourceLocation ON SourceLocation.Inventory = Inventory.SourceInventory) 
    LEFT JOIN dbo.Locations AS LocationFrom ON LocationFrom.Location = SourceLocation.Location) 

    LEFT JOIN dbo.Inventory AS TargetLocation ON TargetLocation.Inventory = Inventory.TargetInventory) 
    LEFT JOIN dbo.Locations AS LocationTo ON LocationTo.Location = TargetLocation.Location) 

WHERE 
    (Inventory.SourceType = 'Q' OR Inventory.SourceType = 'G' OR Inventory.SourceType = 'P' OR Inventory.SourceType = 'A' OR Inventory.SourceType = 'B') 
    AND 
    ((Inventory.EffectiveDate >= 2011-12-30 And Inventory.EffectiveDate <= 2011-12-31)); 

该查询从Excel罚款运行。但我一直在寻找能够查看表格的工具,这就是为什么我使用Access的原因 - 但它给了我更多的问题......

回答

1

您需要用单引号括住日期参数。

Inventory.EffectiveDate >= '2011-12-30' 

您还应该考虑使用较短的别名来使代码更加简洁。我不确定使用像Products这样的别名来代表dbo.Products ......您还应该消除所有不必要的括号,如果Access不强制他们在您身上。

SET NOCOUNT ON; 

SELECT 
    inv.EffectiveDate, 
    inv.Quantity, 
    inv.SourceType, 
    inv.PickingLocation, 
    inv.SourceInventory, 
    loc.LocationId, 
    cust.CustomerName, 
    prod.ProductId, 
    lFrom.LocationId as lFrom, 
    lTo.LocationId as lTo 
FROM dbo.Inventory AS inv 
LEFT OUTER JOIN dbo.Products AS prod ON prod.Product = inv.Product 
LEFT OUTER JOIN dbo.Locations AS loc ON loc.Location = inv.Location 
LEFT OUTER JOIN dbo.Customers AS cust ON inv.Location = cust.ConsignmentLocation 
LEFT OUTER JOIN dbo.Inventory AS src ON src.Inventory = inv.SourceInventory 
LEFT OUTER JOIN dbo.Locations AS lFrom ON lFrom.Location = src.Location 
LEFT OUTER JOIN dbo.Inventory AS trg ON trg.Inventory = inv.TargetInventory 
LEFT OUTER JOIN dbo.Locations AS lTo ON lTo.Location = trg.Location 
WHERE 
    inv.SourceType IN ('Q', 'G', 'P', 'A', 'B') 
    AND inv.EffectiveDate >= '2011-12-30' 
    AND inv.EffectiveDate <= '2011-12-31'; -- suspect you want < '2012-01-01' here 
    -- unless your column doesn't store time. 
+0

它实际上是相反的。如果我把它们放在引号中,我得到这个错误:'char数据类型转换为smalldaretime导致超出范围值' – Elen 2012-02-23 17:38:58

+0

'Inventory.EffectiveDate'的数据类型是什么?如果您使用'YYYYMMDD'而不是'YYYY-MM-DD',会怎么样? – 2012-02-23 17:48:51

+0

你引导我走向正确的方向!显然它应该看起来像'30/12/11''谢谢你! – Elen 2012-02-23 17:56:46