SET ANSI_NULLS (Transact-SQL)
Applies To: SQL Server 2014, SQL Server 2016 Preview
Topic Status: Some information in this topic is preview and subject to change in future releases. Preview information describes new features or changes to existing features in Microsoft SQL Server 2016 Community Technology Preview 2 (CTP2).
Specifies ISO compliant behavior of the Equals (=) and Not Equal To (<>) comparison operators when they are used with null values in SQL Server 2016.
In a future version of SQL Server, ANSI_NULLS will always be ON and any applications that explicitly set the option to OFF will generate an error. Avoid using this feature in new development work, and plan to modify applications that currently use this feature.
Applies to: SQL Server (SQL Server 2008 through current version), Azure SQL Data Warehouse Public Preview.
When SET ANSI_NULLS is ON, a SELECT statement that uses WHERE column_name = NULL returns zero rows even if there are null values in column_name. A SELECT statement that uses WHERE column_name <> NULL returns zero rows even if there are nonnull values in column_name.
When SET ANSI_NULLS is OFF, the Equals (=) and Not Equal To (<>) comparison operators do not follow the ISO standard. A SELECT statement that uses WHERE column_name = NULL returns the rows that have null values in column_name. A SELECT statement that uses WHERE column_name <> NULL returns the rows that have nonnull values in the column. Also, a SELECT statement that uses WHERE column_name <> XYZ_value returns all rows that are not XYZ_value and that are not NULL.
When SET ANSI_NULLS is ON, all comparisons against a null value evaluate to UNKNOWN. When SET ANSI_NULLS is OFF, comparisons of all data against a null value evaluate to TRUE if the data value is NULL. If SET ANSI_NULLS is not specified, the setting of the ANSI_NULLS option of the current database applies. For more information about the ANSI_NULLS database option, see ALTER DATABASE (Transact-SQL).
SET ANSI_NULLS ON affects a comparison only if one of the operands of the comparison is either a variable that is NULL or a literal NULL. If both sides of the comparison are columns or compound expressions, the setting does not affect the comparison.
For a script to work as intended, regardless of the ANSI_NULLS database option or the setting of SET ANSI_NULLS, use IS NULL and IS NOT NULL in comparisons that might contain null values.
SET ANSI_NULLS should be set to ON for executing distributed queries.
SET ANSI_NULLS must also be ON when you are creating or changing indexes on computed columns or indexed views. If SET ANSI_NULLS is OFF, any CREATE, UPDATE, INSERT, and DELETE statements on tables with indexes on computed columns or indexed views will fail. SQL Server will return an error that lists all SET options that violate the required values. Also, when you execute a SELECT statement, if SET ANSI_NULLS is OFF, SQL Server will ignore the index values on computed columns or views and resolve the select operation as if there were no such indexes on the tables or views.
ANSI_NULLS is one of seven SET options that must be set to required values when dealing with indexes on computed columns or indexed views. The options ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, and CONCAT_NULL_YIELDS_NULL must also be set to ON, and NUMERIC_ROUNDABORT must be set to OFF.
The SQL Server Native Client ODBC driver and SQL Server Native Client OLE DB Provider for SQL Server automatically set ANSI_NULLS to ON when connecting. This setting can be configured in ODBC data sources, in ODBC connection attributes, or in OLE DB connection properties that are set in the application before connecting to an instance of SQL Server. The default for SET ANSI_NULLS is OFF.
When SET ANSI_DEFAULTS is ON, SET ANSI_NULLS is enabled.
The setting of SET ANSI_NULLS is set at execute or run time and not at parse time.
To view the current setting for this setting, run the following query.
DECLARE @ANSI_NULLS VARCHAR(3) = 'OFF'; IF ( (32 & @@OPTIONS) = 32 ) SET @ANSI_ NULLS = 'ON'; SELECT @ANSI_ NULLS AS ANSI_ NULLS;
The following example uses the Equals (=) and Not Equal To (<>) comparison operators to make comparisons with NULL and nonnull values in a table. The example also shows that IS NULL is not affected by the SET ANSI_NULLS setting.
-- Create table t1 and insert values. CREATE TABLE dbo.t1 (a INT NULL); INSERT INTO dbo.t1 values (NULL),(0),(1); GO -- Print message and perform SELECT statements. PRINT 'Testing default setting'; DECLARE @varname int; SET @varname = NULL; SELECT a FROM t1 WHERE a = @varname; SELECT a FROM t1 WHERE a <> @varname; SELECT a FROM t1 WHERE a IS NULL; GO -- SET ANSI_NULLS to ON and test. PRINT 'Testing ANSI_NULLS ON'; SET ANSI_NULLS ON; GO DECLARE @varname int; SET @varname = NULL SELECT a FROM t1 WHERE a = @varname; SELECT a FROM t1 WHERE a <> @varname; SELECT a FROM t1 WHERE a IS NULL; GO -- SET ANSI_NULLS to OFF and test. PRINT 'Testing SET ANSI_NULLS OFF'; SET ANSI_NULLS OFF; GO DECLARE @varname int; SET @varname = NULL; SELECT a FROM t1 WHERE a = @varname; SELECT a FROM t1 WHERE a <> @varname; SELECT a FROM t1 WHERE a IS NULL; GO -- Drop table t1. DROP TABLE dbo.t1;