|
|
|
SSC Eights!
      
Group: General Forum Members
Last Login: Tuesday, May 21, 2013 4:41 AM
Points: 909,
Visits: 596
|
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Monday, May 13, 2013 11:21 AM
Points: 2,163,
Visits: 2,148
|
|
Wow, I happened to be the first to answer it, and I got it right. 
Though not for the reason you stated, as it makes no difference if ANSI_NULLS is set ON or OFF that CASE statement will always return EQUAL, as NULL is always IS NULL. (Or at least as far as I can tell.)
If the CASE statement had been NULL = NULL the story would have been different.
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Today @ 7:51 AM
Points: 1,852,
Visits: 986
|
|
good question.Excellent explanation.
Malleswarareddy I.T.Analyst MCITP(70-451)
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Today @ 7:51 AM
Points: 1,852,
Visits: 986
|
|
UMG Developer (6/1/2010)
Wow, I happened to be the first to answer it, and I got it right.  Though not for the reason you stated, as it makes no difference if ANSI_NULLS is set ON or OFF that CASE statement will always return EQUAL, as NULL is always IS NULL. (Or at least as far as I can tell.) If the CASE statement had been NULL = NULL the story would have been different.
SET ANSI_NULLS OFF SET XACT_ABORT ON GO CREATE PROCEDURE DBO.x AS SET ANSI_NULLS OFF SELECT CASE WHEN NULL = NULL THEN 'EQUAL' ELSE 'NOT EQUAL' END AS ANSWER SET ANSI_NULLS ON SELECT CASE WHEN NULL = NULL THEN 'EQUAL' ELSE 'NOT EQUAL' END AS ANSWER GO SET ANSI_NULLS OFF EXEC DBO.X SET ANSI_NULLS ON EXEC DBO.X DROP PROCEDURE DBO.X
This also gives the same answer please check it.and read explanation carefully.
Malleswarareddy I.T.Analyst MCITP(70-451)
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Monday, May 13, 2013 11:21 AM
Points: 2,163,
Visits: 2,148
|
|
malleswarareddy_m (6/1/2010)
UMG Developer (6/1/2010)
Wow, I happened to be the first to answer it, and I got it right.  Though not for the reason you stated, as it makes no difference if ANSI_NULLS is set ON or OFF that CASE statement will always return EQUAL, as NULL is always IS NULL. (Or at least as far as I can tell.) If the CASE statement had been NULL = NULL the story would have been different. This also gives the same answer please check it.and read explanation carefully.
But like I said the explanation has nothing to do with the results in this example. Change all of them to SET ANSI_NULLS ON and try it, then set all of them to SET ANSI_NULLS OFF and try it and you will get the same results of EQUAL for everything. What he says may be true about how the settings sticks with the SP, but it doesn't change the results one bit.
For example try running this:
SET ANSI_NULLS ON; SELECT CASE WHEN NULL IS NULL THEN 'EQUAL' ELSE 'NOT EQUAL' END AS ANSWER; SET ANSI_NULLS OFF; SELECT CASE WHEN NULL IS NULL THEN 'EQUAL' ELSE 'NOT EQUAL' END AS ANSWER; You will get EQUAL for both, but with this:
SET ANSI_NULLS ON; SELECT CASE WHEN NULL = NULL THEN 'EQUAL' ELSE 'NOT EQUAL' END AS ANSWER; SET ANSI_NULLS OFF; SELECT CASE WHEN NULL = NULL THEN 'EQUAL' ELSE 'NOT EQUAL' END AS ANSWER; you will not. (At least on my SQL Server 2008 R2 instance.)
What I am trying to say is that the ANSI_NULLS setting will not change how "NULL IS NULL" is evaluated, but it will change how "NULL = NULL" is evaluated.
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Today @ 7:51 AM
Points: 1,852,
Visits: 986
|
|
UMG Developer (6/1/2010)
malleswarareddy_m (6/1/2010)
UMG Developer (6/1/2010)
Wow, I happened to be the first to answer it, and I got it right.  Though not for the reason you stated, as it makes no difference if ANSI_NULLS is set ON or OFF that CASE statement will always return EQUAL, as NULL is always IS NULL. (Or at least as far as I can tell.) If the CASE statement had been NULL = NULL the story would have been different. This also gives the same answer please check it.and read explanation carefully. But like I said the explanation has nothing to do with the results in this example. Change all of them to SET ANSI_NULLS ON and try it, then set all of them to SET ANSI_NULLS OFF and try it and you will get the same results of EQUAL for everything. What he says may be true about how the settings sticks with the SP, but it doesn't change the results one bit. For example try running this: SET ANSI_NULLS ON; SELECT CASE WHEN NULL IS NULL THEN 'EQUAL' ELSE 'NOT EQUAL' END AS ANSWER; SET ANSI_NULLS OFF; SELECT CASE WHEN NULL IS NULL THEN 'EQUAL' ELSE 'NOT EQUAL' END AS ANSWER; You will get EQUAL for both, but with this: SET ANSI_NULLS ON; SELECT CASE WHEN NULL = NULL THEN 'EQUAL' ELSE 'NOT EQUAL' END AS ANSWER; SET ANSI_NULLS OFF; SELECT CASE WHEN NULL = NULL THEN 'EQUAL' ELSE 'NOT EQUAL' END AS ANSWER; you will not. (At least on my SQL Server 2008 R2 instance.) What I am trying to say is that the ANSI_NULLS setting will not change how "NULL IS NULL" is evaluated, but it will change how "NULL = NULL" is evaluated.
you are correct
Malleswarareddy I.T.Analyst MCITP(70-451)
|
|
|
|
|
SSC Eights!
      
Group: General Forum Members
Last Login: Tuesday, May 21, 2013 4:41 AM
Points: 909,
Visits: 596
|
|
Absolutely right - my bad. I'm afraid my brain is wired to never ever use '=' or '<>' where either side can be null, even if I'm trying to make a point about exactly that
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Monday, September 17, 2012 7:30 AM
Points: 1,038,
Visits: 679
|
|
| if you use IS NULL then there will not be any effect on the ANSI_NULLS settings
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Friday, May 17, 2013 2:53 AM
Points: 1,530,
Visits: 359
|
|
sharath.chalamgari (6/2/2010) if you use IS NULL then there will not be any effect on the ANSI_NULLS settings
agreed... i executed the query on sql 2005; ANSI NULLS on/off doesn't make any difference.
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Today @ 2:05 AM
Points: 1,103,
Visits: 1,200
|
|
Most difficult thing with this question was thinking where the trick is hidden. I understand author has a block to write null=null
See, understand, learn, try, use efficient © Dr.Plch
|
|
|
|