T-SQL not in 遇到 null 暗含的陷阱

时间:2021-02-11 09:31:46

下面有两个T-SQL,乍看之下以为是等效的,实际却不是这样

                         SELECT orderid1 ,
LocalCost ,
supplyGoodsName ,
buyersgoodsid ,
JSitId ,
GSitId
FROM csc_result WITH ( NOLOCK )
WHERE [level] > 0
AND orderid1 LIKE '618464266%'
AND ( supplygoodsid NOT IN ( 1085317, 1112957 ) )

T-SQL not in 遇到 null 暗含的陷阱

                         SELECT orderid1,
LocalCost ,
supplyGoodsName ,
buyersgoodsid ,
JSitId ,
GSitId
FROM csc_result WITH ( NOLOCK )
WHERE [level] > 0
AND orderid1 LIKE '618464266%'
AND (supplygoodsid NOT IN ( 1085317,
1112957 )
OR SupplyGoodsId IS NULL)

T-SQL not in 遇到 null 暗含的陷阱

可见not in并不能把null值排除在外