|
|
|
Old Hand
      
Group: General Forum Members
Last Login: Monday, May 20, 2013 10:23 AM
Points: 350,
Visits: 455
|
|
| Comments posted to this topic are about the item Nulls
|
|
|
|
|
SSC Eights!
      
Group: General Forum Members
Last Login: Monday, March 25, 2013 4:03 AM
Points: 850,
Visits: 71
|
|
Good question! But missed the Batch Separator(GO(default) or ;) between the Insert statements.
INSERT INTO dbo.Employees(EmployeeID) values (1) GO INSERT INTO dbo.Employees(EmployeeID) values (2) GO INSERT INTO dbo.Employees(EmployeeID) values (3) GO INSERT INTO dbo.Employees(EmployeeID) values (4) GO
Regards, Jagan.
|
|
|
|
|
UDP Broadcaster
      
Group: General Forum Members
Last Login: Friday, May 10, 2013 3:15 AM
Points: 1,476,
Visits: 1,943
|
|
jaganmohan.rao (12/7/2010) Good question! But missed the Batch Separator(GO(default) or ;) between the Insert statements.
INSERT INTO dbo.Employees(EmployeeID) values (1) GO INSERT INTO dbo.Employees(EmployeeID) values (2) GO INSERT INTO dbo.Employees(EmployeeID) values (3) GO INSERT INTO dbo.Employees(EmployeeID) values (4) GO
Which really arnt needed. None of them are (atleast not if your using SSMS).
But i also liked the question. Since it wasnt that long ago i ran into this somewhere on this site (QOD/Post/Article... something). And refreshing that now makes it much more likely that i will actually remember it 
/T
|
|
|
|
|
SSC Journeyman
      
Group: General Forum Members
Last Login: Thursday, February 17, 2011 12:19 AM
Points: 77,
Visits: 33
|
|
Yes, question is instrustive (for me), but BOL says:
Caution: 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 So, results may be different! By the way, using Set ansi_nulls off, the result is three rows: "2, 3, 4"
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: 2 days ago @ 6:14 AM
Points: 1,102,
Visits: 1,197
|
|
savosin_sergey (12/7/2010)
Yes, question is instrustive (for me), but BOL says: Caution: 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 So, results may be different! By the way, using Set ansi_nulls off, the result is three rows: "2, 3, 4" ANSI_NULLS settings are discussed every time null values are mentioned. If someone wants to make it a point of the question, they can add the "It depands" answer. Null values in negative comparisons are allways dangerous.
See, understand, learn, try, use efficient © Dr.Plch
|
|
|
|
|
Hall of Fame
       
Group: General Forum Members
Last Login: Today @ 12:38 AM
Points: 3,191,
Visits: 4,148
|
|
Good question.
But this is not an explanation at all:
Explanation: If a subquery returns any null value the main query will not return any rows
I suggest the following explanation.
The SELECT query can be modified as follows:
select * from dbo.Employees where EmployeeID not in (SELECT NullableColumn FROM NullOperation) -->
select * from dbo.Employees where EmployeeID not in (NULL, 0, 1) -->
select * from dbo.Employees where (EmployeeID <> NULL) and (EmployeeID <> 0) and (EmployeeID <> 1) The result of the comparison "EmployeeID <> NULL" is undefined for all values of EmployeeID (unless you set the ANSI_NULLS option to OFF). So the result of the WHERE clause is undefined (i.e. not true), and no rows are returned.
|
|
|
|
|
SSC Veteran
      
Group: General Forum Members
Last Login: Wednesday, May 15, 2013 7:00 AM
Points: 258,
Visits: 166
|
|
Interesting question. But at the least I will have to question the explanation ("If a subquery returns any null value the main query will not return any rows"). If you change the query to:
select * from dbo.Employees where EmployeeID in (SELECT NullableColumn FROM NullOperation)
Then a single row is returned (1), even though the subquery still returns a null value. So I think a better explanation is that the IN operator returns NULL if the value is not found in the list, and the list contains NULL values.
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 2:00 AM
Points: 5,238,
Visits: 7,045
|
|
Good question. But the explanation is not an explanation at all. I wrote a series of blog posts about NULLs, and one part covers (a.o.) this issue - you can find it at http://sqlblog.com/blogs/hugo_kornelis/archive/2007/09/22/dr-unknown-or-how-i-learned-to-stop-worrying-and-love-the-null.aspx. (I recommend reading the other three parts as well - links to parts 1 and 2 are in the opening paragraph, and a link to part 4 is in the comments).
savosin_sergey (12/7/2010)
Yes, question is instrustive (for me), but BOL says: Caution: 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 So, results may be different! By the way, using Set ansi_nulls off, the result is three rows: "2, 3, 4" Results are unexpected, not undefined or unpredictable. Unexpected means that the results may not match human expectation, not that results may vary. ANSI_NULLS is on by default, and ANSI_NULLS OFF is deprecated, so I think it's fair to assume ANSI_NULLS on unless explicitly told otherwise - except, maybe, when the author includes "it depends" in the answer options.
Hugo Kornelis, SQL Server MVP Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
|
|
|
|
|
SSCrazy Eights
        
Group: General Forum Members
Last Login: Today @ 1:21 AM
Points: 9,374,
Visits: 6,471
|
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Friday, May 17, 2013 2:53 AM
Points: 1,528,
Visits: 359
|
|
mistakenly clicked on the NULLS answer... 
nice question, thanks.
|
|
|
|