SET ANSI_NULL_DFLT_OFF (Transact-SQL)
Applies to: SQL Server Azure SQL Database Azure SQL Managed Instance Azure Synapse Analytics Analytics Platform System (PDW) SQL analytics endpoint in Microsoft Fabric Warehouse in Microsoft Fabric
Alters the behavior of the session to override default nullability of new columns when the ANSI null default option for the database is true. For more information about setting the value for ANSI null default, see ALTER DATABASE (Transact-SQL).
Transact-SQL syntax conventions
Syntax
-- Syntax for SQL Server and Azure SQL Database and Microsoft Fabric
SET ANSI_NULL_DFLT_OFF { ON | OFF }
-- Syntax for Azure Synapse Analytics and Parallel Data Warehouse
SET ANSI_NULL_DFLT_OFF OFF
Remarks
This setting only affects the nullability of new columns when the nullability of the column is not specified in the CREATE TABLE and ALTER TABLE statements. By default, when SET ANSI_NULL_DFLT_OFF is ON, new columns that are created by using the ALTER TABLE and CREATE TABLE statements are NOT NULL if the nullability status of the column is not explicitly specified. SET ANSI_NULL_DFLT_OFF does not affect columns that are created by using an explicit NULL or NOT NULL.
Both SET ANSI_NULL_DFLT_OFF and SET ANSI_NULL_DFLT_ON cannot be set ON at the same time. If one option is set ON, the other option is set OFF. Therefore, either ANSI_NULL_DFLT_OFF or SET ANSI_NULL_DFLT_ON can be set ON, or both can be set OFF. If either option is ON, that setting (SET ANSI_NULL_DFLT_OFF or SET ANSI_NULL_DFLT_ON) takes effect. If both options are set OFF, SQL Server uses the value of the is_ansi_null_default_on column in the sys.databases catalog view.
For a more reliable operation of Transact-SQL scripts that are used in databases with different nullability settings, it is better to always specify NULL or NOT NULL in CREATE TABLE and ALTER TABLE statements.
The setting of SET ANSI_NULL_DFLT_OFF 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_NULL_DFLT_OFF VARCHAR(3) = 'OFF';
IF ( (2048 & @@OPTIONS) = 2048 ) SET @ANSI_NULL_DFLT_OFF = 'ON';
SELECT @ANSI_NULL_DFLT_OFF AS ANSI_NULL_DFLT_OFF;
Permissions
Requires membership in the public role.
Examples
The following example shows the effects of SET ANSI_NULL_DFLT_OFF
with both settings for the ANSI null default database option.
USE AdventureWorks2022;
GO
-- Set the 'ANSI null default' database option to true by executing
-- ALTER DATABASE.
GO
ALTER DATABASE AdventureWorks2022 SET ANSI_NULL_DEFAULT ON;
GO
-- Create table t1.
CREATE TABLE t1 (a TINYINT);
GO
-- NULL INSERT should succeed.
INSERT INTO t1 (a) VALUES (NULL);
GO
-- SET ANSI_NULL_DFLT_OFF to ON and create table t2.
SET ANSI_NULL_DFLT_OFF ON;
GO
CREATE TABLE t2 (a TINYINT);
GO
-- NULL INSERT should fail.
INSERT INTO t2 (a) VALUES (NULL);
GO
-- SET ANSI_NULL_DFLT_OFF to OFF and create table t3.
SET ANSI_NULL_DFLT_OFF OFF;
GO
CREATE TABLE t3 (a TINYINT) ;
GO
-- NULL INSERT should succeed.
INSERT INTO t3 (a) VALUES (NULL);
GO
-- This illustrates the effect of having both the database
-- option and SET option disabled.
-- Set the 'ANSI null default' database option to false.
ALTER DATABASE AdventureWorks2022 SET ANSI_NULL_DEFAULT OFF;
GO
-- Create table t4.
CREATE TABLE t4 (a TINYINT) ;
GO
-- NULL INSERT should fail.
INSERT INTO t4 (a) VALUES (NULL);
GO
-- SET ANSI_NULL_DFLT_OFF to ON and create table t5.
SET ANSI_NULL_DFLT_OFF ON;
GO
CREATE TABLE t5 (a TINYINT);
GO
-- NULL insert should fail.
INSERT INTO t5 (a) VALUES (NULL);
GO
-- SET ANSI_NULL_DFLT_OFF to OFF and create table t6.
SET ANSI_NULL_DFLT_OFF OFF;
GO
CREATE TABLE t6 (a TINYINT);
GO
-- NULL insert should fail.
INSERT INTO t6 (a) VALUES (NULL);
GO
-- Drop tables t1 through t6.
DROP TABLE t1, t2, t3, t4, t5, t6;
See Also
ALTER TABLE (Transact-SQL)
CREATE TABLE (Transact-SQL)
SET Statements (Transact-SQL)
SET ANSI_NULL_DFLT_ON (Transact-SQL)