1

次のSQLステートメントが機能していません。

SELECT  stock.IdStock
        ,stock.Descr       
FROM    [Inventory].[dbo].[Stock] stock
WHERE   stock.IdStock NOT IN
        (SELECT foreignStockId FROM
         [Subset].[dbo].[Products])
4

2 に答える 2

4

おそらくを返しますNULL。試す

SELECT  stock.IdStock
        ,stock.Descr       
FROM    [Inventory].[dbo].[Stock] stock
WHERE   stock.IdStock NOT IN
        (SELECT foreignStockId FROM
         [Subset].[dbo].[Products]
         WHERE foreignStockId IS NOT NULL )

私は通常、このタイプのクエリにNOTEXISTSを使用します

SELECT stock.idstock,
       stock.descr
FROM   [Inventory].[dbo].[Stock] stock
WHERE  NOT EXISTS (SELECT *
                   FROM   [Subset].[dbo].[Products] p
                   WHERE  p.foreignstockid = stock.idstock)
于 2012-11-19T10:23:52.390 に答える
2

おそらく

SELECT foreignStockId FROM
         [Subset].[dbo].[Products]

クエリは少なくとも1つのNULL値を返します

EXISTSをお試しください

SELECT  stock.IdStock
        ,stock.Descr       
FROM    [Inventory].[dbo].[Stock] stock
WHERE   NOT EXISTS
        (SELECT * FROM [Subset].[dbo].[Products] 
         where stock.IdStock =foreignStockId )
于 2012-11-19T10:23:10.767 に答える