Bagikan melalui


SET ANSI_NULL_DFLT_OFF (Transact-SQL)

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) and Setting Database Options.

Topic link iconTransact-SQL Syntax Conventions

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. 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.

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 AdventureWorks;
GO

-- Set the 'ANSI null default' database option to true by executing 
-- ALTER DATABASE.
GO
ALTER DATABASE AdventureWorks 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 AdventureWorks 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
DROP TABLE t2
DROP TABLE t3
DROP TABLE t4
DROP TABLE t5
DROP TABLE t6