2012-02-23 26 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の罰金から実行されます。しかし、私はテーブルを見ることができるツールを探していたので、私はアクセスを使用しています - それはより多くの問題をもたらします....

答えて

1

あなたの日付パラメータを一重引用符で囲む必要があります。

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

コードを簡潔にするために短いエイリアスを使用することも検討する必要があります。私は、Productsのようなエイリアスを使用してdbo.Productsを表す目的を確信していません。もしあなたがそれらを強制しなければ、不要なカッコもすべて削除するべきです。

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

+0

' Inventory.EffectiveDate'のデータ型は? 'YYYY-MM-DD'の代わりに' YYYYMMDD'を使うとどうなりますか? –

+0

あなたは正しい方向に私を導いた!明らかに '' 30/12/11''のように見えるはずです! – Elen

関連する問題