Transact-SQL Reference

SET ANSI_NULL_DFLT_OFF

Alters the session's behavior 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 sp_dboption and Setting Database Options.

Syntax

SET ANSI_NULL_DFLT_OFF {ON | 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. When SET ANSI_NULL_DFLT_OFF is ON, new columns created with the ALTER TABLE and CREATE TABLE statements are, by default, NOT NULL if the nullability status of the column is not explicitly specified. SET ANSI_NULL_DFLT_OFF has no effect on columns created with an explicit NULL or NOT NULL.

Both SET ANSI_NULL_DFLT_OFF and SET ANSI_NULL_DFLT_ON cannot be set ON simultaneously. 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, Microsoft® SQL Server™ uses the value of the ANSI null default option of sp_dboption.

For the most reliable operation of Transact-SQL scripts that may be used in databases with different nullability settings, it is best 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.

Permissions

SET ANSI_NULL_DFLT_OFF permissions default to all users.

Examples

This example shows the effects of SET ANSI_NULL_DFLT_OFF with both settings for the ANSI null default database option.

USE pubs
GO
-- Set the 'ANSI null default' database option to true by executing 
-- sp_dboption.
GO
EXEC sp_dboption 'pubs','ANSI null default','true'
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 sp_dboption and SET 
-- option disabled.
-- Set the 'ANSI null default' database option to false.
EXEC sp_dboption 'pubs','ANSI null default','false'
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
DROP TABLE t2
DROP TABLE t3
DROP TABLE t4
DROP TABLE t5
DROP TABLE t6
GO

See Also

ALTER TABLE

CREATE TABLE

SET

SET ANSI_NULL_DFLT_ON