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 )

对于这种类型的查询,我通常使用 NOT EXISTS

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]

查询返回至少一个 NULL 值

尝试存在

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 回答