2009-09-20 65 views
0

我有以下查詢SQL Server 2008中 - 不包括行

DECLARE @ProductIdsToExclude爲nvarchar(最大)

SET @ProductIdsToExclude =「49506541-4CE2-40AC-812A-7AB262E6F0B0,49506541-4ce2- 40ac-812A-7ab262e6f0b0'

然後我用this功能解析@ProductIdsToExclude到一個臨時表:

CREATE TABLE

#TempProductIdsToExclude(產品編號唯一標識符)

INSERT

#TempProductIdsToExclude(產品編號)

SELECT

t.txt_value

FROM

dbo.fn_ParseText2Table(@ProductIdsToExclude, '')爲t

我然後用這個查詢撤出所有產品

SELECT * FROM產品

我的問題是 - 我怎樣才能得到查詢到排除所有結果在ProductId產品表包含在#TempProductIdsToExclude

謝謝!

回答

2

使用where not exists

select * 
    from Products prod with (nolock) 
where not exists (select 1 
        from #TempProductIdsToExclude temp 
        where temp.ProductId = prod.ProductId) 
+0

感謝 - 工程一種享受! – db1234 2009-09-20 11:06:07

+0

出於好奇,爲何使用「with(nolock)」? – Diego 2009-09-22 08:59:25

+0

默認情況下,整個'產品'表將在'select'運行時被鎖定,即沒有其他查詢可以訪問它。 'with(nolock)'禁用此鎖定以允許併發訪問 - 缺點是,如果表格數據在「select」運行時發生更改,則結果可能與您所期望的不同。 – 2009-09-22 13:51:51

0

你可以使用:

select * from products where productId not in (select productId from) #TempProductIdsToExclude)