SET ANSI_NULLS ON/OFF
QUOTED_IDENTIFIER
QUOTED_IDENTIFIER
This setting controls how quotation marks
".."
are interpreted by the SQL compiler. When QUOTED_IDENTIFIER
is ON then quotes are treated like brackets ([...]
) and can be used to quote SQL object names like table names, column names, etc. When it is OFF (not recommended), then quotes are treated like apostrophes ('..'
) and can be used to quote text strings in SQL commands.ANSI_NULLS
This setting controls what happens when you try to use any comparison operator other than
IS
on NULL. When it is ON, these comparisons follow the standard which says that comparing to NULL always fails (because it isn't a value, it's a Flag) and returns FALSE
. When this setting is OFF (really not recommended) you can sucessfully treat it like a value and use =
, <>
, etc. on it and get back TRUE as appropiate.
The proper way to handle this is to instead use the
IS
(ColumnValue IS NULL ..
).In SQL Server, what does “SET ANSI_NULLS ON” mean?
SET ANSI_NULLS ON/OFF: The ANSI_NULLS option specifies that how SQL Server handles the comparison operations with NULL values. When it is set to ON any comparison with NULL using = and <> will yield to false value. And it is the ISO defined standard behavior.
~
Returns one row. So even when both operands are columns,
NULL
does not equal NULL
. And thedocumentation for =
doesn't have anything to say about the operands:When you compare twoNULL
expressions, the result depends on theANSI_NULLS
setting:IfANSI_NULLS
is set toON
, the result isNULL
1, following the ANSI convention that aNULL
(or unknown) value is not equal to anotherNULL
or unknown value.IfANSI_NULLS
is set toOFF
, the result ofNULL
compared toNULL
isTRUE
.ComparingNULL
to a non-NULL
value always results inFALSE
2.
However, both 1 and 2 are incorrect - the result of both comparisons is
UNKNOWN
.
*The cryptic meaning of this text was finally discovered years later. What it actually means is that, for those comparisons, the setting has no effect and it always acts as if the setting were ON. Would have been clearer if it had stated that
SET ANSI_NULLS OFF
was the setting that had no affect.
No comments:
Post a Comment