MS Access / SQL Server 传递查询

发布于 2025-01-08 17:33:05 字数 1399 浏览 1 评论 0原文

我的查询确实运行,但没有返回结果:

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 的原因 - 但这给我带来了更多问题......

My query does run, but returns no results:

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

This query runs from Excel fine. But I was looking for the tool to be able to see tables, that's why I'm using Access - but it gives me more problems....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

三月梨花 2025-01-15 17:33:05

您需要用单引号将日期参数引起来。

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.

You need to surround your date parameters with single quotes.

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

You should also consider using shorter aliases to make the code more concise. I'm not sure of the purpose of using an alias like Products to represent dbo.Products... you should also eliminate all the unnecessary parentheses if Access doesn't force them on you.

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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文