SET ANSI_NULL_DFLT_OFF (Transact-SQL)

Modifica el comportamiento de la sesión para dejar sin efecto la nulabilidad predeterminada de las columnas nuevas cuando la opción ANSI null default de la base de datos es true. Para obtener más información acerca de cómo establecer el valor de ANSI null default, vea ALTER DATABASE (Transact-SQL) y Configurar las opciones de la base de datos.

Icono de vínculo a temasConvenciones de sintaxis de Transact-SQL

Sintaxis

SET ANSI_NULL_DFLT_OFF { ON | OFF }

Comentarios

Esta opción sólo afecta a la nulabilidad de las columnas nuevas cuando esta nulabilidad no se especifica en las instrucciones CREATE TABLE ni ALTER TABLE. De forma predeterminada, cuando SET ANSI_NULL_DFLT_OFF es ON, las columnas nuevas creadas con las instrucciones ALTER TABLE y CREATE TABLE son NOT NULL si el estado de nulabilidad de la columna no se especifica explícitamente. SET ANSI_NULL_DFLT_OFF no afecta a las columnas creadas con una opción NULL o NOT NULL explícita.

No es posible establecer ON a la vez en SET ANSI_NULL_DFLT_OFF y SET ANSI_NULL_DFLT_ON. Si se establece una de las opciones en ON, la otra se establece en OFF. Por tanto, sólo es posible establecer ON en SET ANSI_NULL_DFLT_OFF o en SET ANSI_NULL_DFLT_ON, o establecer OFF en ambas. Si alguna de estas dos opciones es ON (SET ANSI_NULL_DFLT_OFF o SET ANSI_NULL_DFLT_ON), tendrá efecto la opción correspondiente. Si ambas opciones están establecidas en OFF, SQL Server utiliza el valor de la columna is_ansi_null_default_on en la vista de catálogo sys.databases.

Para conseguir la máxima confiabilidad en los scripts Transact-SQL que se utilicen en bases de datos con distintas opciones de nulabilidad, se recomienda especificar siempre NULL o NOT NULL en las instrucciones CREATE TABLE y ALTER TABLE.

El valor de SET ANSI_NULL_DFLT_OFF se establece en tiempo de ejecución, no en tiempo de análisis.

Permisos

Debe pertenecer al rol public.

Ejemplos

En el ejemplo siguiente se muestran los efectos de SET ANSI_NULL_DFLT_OFF con los dos valores de la opción de base de datos ANSI null default.

USE AdventureWorks2008R2;
GO

-- Set the 'ANSI null default' database option to true by executing 
-- ALTER DATABASE.
GO
ALTER DATABASE AdventureWorks2008R2 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 AdventureWorks2008R2 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;