SET ANSI_NULL_DFLT_ON (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 false. Para obtener más información sobre la configuración del valor de ANSI null default, vea ALTER DATABASE (Transact-SQL).

Icono de vínculo a temas Convenciones de sintaxis de Transact-SQL

Sintaxis

SET ANSI_NULL_DFLT_ON {ON | OFF}

Comentarios

Esta opción solo afecta a la nulabilidad de las columnas nuevas cuando esta nulabilidad no se especifica en las instrucciones CREATE TABLE ni ALTER TABLE. Cuando SET ANSI_NULL_DFLT_ON es ON, las columnas nuevas creadas con las instrucciones ALTER TABLE y CREATE TABLE admitirán valores NULL si el estado de nulabilidad de la columna no se especifica explícitamente. SET ANSI_NULL_DFLT_ON 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, solo 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 NULL o NOT NULL en las instrucciones CREATE TABLE y ALTER TABLE.

El controlador ODBC de SQL Server Native Client y el proveedor OLE DB de SQL Server Native Client para SQL Server establecen automáticamente ANSI_NULL_DFLT_ON en ON al conectarse. SET ANSI_NULL_DFLT_ON tiene como opción predeterminada OFF en las conexiones desde aplicaciones DB-Library.

Cuando SET ANSI_DEFAULTS es ON, se habilita SET ANSI_NULL_DFLT_ON.

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

La configuración de SET ANSI_NULL_DFLT_ON no se aplica cuando las tablas se crean usando la instrucción SELECT INTO.

Permisos

Debe pertenecer al rol public.

Ejemplos

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

USE AdventureWorks2012;
GO

-- The code from this point on demonstrates that SET ANSI_NULL_DFLT_ON
-- has an effect when the 'ANSI null default' for the database is false.
-- Set the 'ANSI null default' database option to false by executing
-- ALTER DATABASE.
ALTER DATABASE AdventureWorks2012 SET ANSI_NULL_DEFAULT OFF;
GO
-- Create table t1.
CREATE TABLE t1 (a TINYINT) ;
GO 
-- NULL INSERT should fail.
INSERT INTO t1 (a) VALUES (NULL);
GO

-- SET ANSI_NULL_DFLT_ON to ON and create table t2.
SET ANSI_NULL_DFLT_ON ON;
GO
CREATE TABLE t2 (a TINYINT);
GO 
-- NULL insert should succeed.
INSERT INTO t2 (a) VALUES (NULL);
GO

-- SET ANSI_NULL_DFLT_ON to OFF and create table t3.
SET ANSI_NULL_DFLT_ON OFF;
GO
CREATE TABLE t3 (a TINYINT);
GO
-- NULL insert should fail.
INSERT INTO t3 (a) VALUES (NULL);
GO

-- The code from this point on demonstrates that SET ANSI_NULL_DFLT_ON 
-- has no effect when the 'ANSI null default' for the database is true.
-- Set the 'ANSI null default' database option to true.
ALTER DATABASE AdventureWorks2012 SET ANSI_NULL_DEFAULT ON
GO

-- Create table t4.
CREATE TABLE t4 (a TINYINT);
GO 
-- NULL INSERT should succeed.
INSERT INTO t4 (a) VALUES (NULL);
GO

-- SET ANSI_NULL_DFLT_ON to ON and create table t5.
SET ANSI_NULL_DFLT_ON ON;
GO
CREATE TABLE t5 (a TINYINT);
GO 
-- NULL INSERT should succeed.
INSERT INTO t5 (a) VALUES (NULL);
GO

-- SET ANSI_NULL_DFLT_ON to OFF and create table t6.
SET ANSI_NULL_DFLT_ON OFF;
GO
CREATE TABLE t6 (a TINYINT);
GO 
-- NULL INSERT should succeed.
INSERT INTO t6 (a) VALUES (NULL);
GO

-- Set the 'ANSI null default' database option to false.
ALTER DATABASE AdventureWorks2012 SET ANSI_NULL_DEFAULT ON;
GO

-- Drop tables t1 through t6.
DROP TABLE t1,t2,t3,t4,t5,t6;

Vea también

Referencia

ALTER TABLE (Transact-SQL)

CREATE TABLE (Transact-SQL)

Instrucciones SET (Transact-SQL)

SET ANSI_DEFAULTS (Transact-SQL)

SET ANSI_NULL_DFLT_OFF (Transact-SQL)