Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

When using NOT IN subquery results are corrupt when null value is present

$
0
0

I found this caution statement in help:

Any null values returned by subquery or expression that are compared to test_expression using IN or NOT IN return UNKNOWN. Using null values in together with IN or NOT IN can produce unexpected results.

However, I feel this should be on the bug not caution list.  It was a bug to us. Do we need to go to help and look for cautions for every operator or function our team uses in SQL Server.  This issue can be reproduced below.  A developer executing this code should expect valid results not unexpected results and they don't have time to be worrying about whether or not a query is going to work.  Or, just dump the NOT and NOT IN operators in the next release.  Thank You! 

if exists (select * from sys.tables where name = N'T1') drop table [T1]
if exists (select * from sys.tables where name = N'T2') drop table [T2]

create table [t1]([c1] [int] identity(1,1) not null, [c2] [int] null, constraint [pk_t1] primary key clustered ( [c1] asc )with (pad_index = off, statistics_norecompute = off, ignore_dup_key = off, allow_row_locks = on, allow_page_locks = on) on [primary] ) on [primary]
create table [t2]([c1] [int] identity(1,1) not null, [c2] [int] null, constraint [pk_test_null_values_in_sub_query] primary key clustered ( [c1] asc )with (pad_index = off, statistics_norecompute = off, ignore_dup_key = off, allow_row_locks = on, allow_page_locks = on) on [primary] ) on [primary]

insert into [t1]([c2])values(1)
insert into [t1]([c2])values(2)
insert into [t1]([c2])values(3)
insert into [t1]([c2])values(4)
insert into [t1]([c2])values(5)

--value three is not present in table 2
insert into [t2]([c2])values(1)
insert into [t2]([c2])values(2)
insert into [t2]([c2])values(null)
insert into [t2]([c2])values(4)
insert into [t2]([c2])values(5)

--this doesn't work
select [c1], [c2] from [t1] where [c2] not in (select [c2] from [t2])

--this works when the null value is filtered
select [c1], [c2] from [t1] where [c2] not in (select [c2] from [t2] where [c2] is not null)


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>