Nota:
El acceso a esta página requiere autorización. Puede intentar iniciar sesión o cambiar directorios.
El acceso a esta página requiere autorización. Puede intentar cambiar los directorios.
Se aplica a:SQL Server
Azure SQL Database
Instancia administrada de Azure SQL
Azure Synapse Analytics
Analytics Platform System (PDW)
Punto de conexión de SQL Analytics en Microsoft Fabric
Almacén en Microsoft Fabric
Base de datos SQL en Microsoft Fabric
Modifica el comportamiento de la sesión para invalidar la nulabilidad predeterminada de las columnas nuevas cuando la opción ANSI null default de la base de datos es false. Para más información sobre cómo establecer el valor para el valor por defecto nulo ANSI, véaseALTER DATABASE (Transact-SQL).
Convenciones de sintaxis de Transact-SQL
Sintaxis
-- Syntax for SQL Server and Azure SQL Database and Microsoft Fabric
SET ANSI_NULL_DFLT_ON {ON | OFF}
-- Syntax for Azure Synapse Analytics and Parallel Data Warehouse
SET ANSI_NULL_DFLT_ON ON
Comentarios
Esta configuración solo afecta a la nulidad de nuevas columnas cuando la nulidad de la columna no está especificada en las CREATE TABLE sentencias y ALTER TABLE . Cuando SETSET ANSI_NULL_DFLT_ON está activado, las nuevas columnas creadas usando las ALTER TABLE sentencias y CREATE TABLE permiten valores nulos si el estado de anulabilidad de la columna no se especifica explícitamente. SET ANSI_NULL_DFLT_ON no afecta a columnas creadas con un NULL explícito o NOT NULL.
Ambos SETSET ANSI_NULL_DFLT_OFF y SETSET ANSI_NULL_DFLT_ON no pueden activarse al mismo tiempo. Si se establece una de las opciones en ON, la otra se establece en OFF. Por lo tanto, puede estar ANSI_NULL_DFLT_OFF activado o ANSI_NULL_DFLT_ON ambos, o ambos puede estar DESACTIVADO. Si alguna de las opciones está ACTIVADA, esa configuración (SETSET ANSI_NULL_DFLT_OFF o SETSET ANSI_NULL_DFLT_ON) entra en vigor. Si ambas opciones están establecidas en OFF, SQL Server usa el valor de la columna is_ansi_null_default_on en la vista de catálogo sys.databases.
Para una operación más fiable de Transact-SQL scripts que se usan en bases de datos con diferentes configuraciones de anulabilidad, es mejor especificar NULL o NOT NULL en CREATE TABLE las sentencias and ALTER TABLE .
El controlador ODBC de SQL Server Native Client y el proveedor OLE DB de SQL Server Native Client para SQL Server se establecen ANSI_NULL_DFLT_ON automáticamente en ON al conectarse. El valor por defecto para SET ANSI_NULL_DFLT_ON OFF para conexiones de DB-Library aplicaciones.
Cuando SETSET ANSI_DEFAULTS está activado, SETSET ANSI_NULL_DFLT_ON está habilitado.
El valor de se establece en tiempo de SET ANSI_NULL_DFLT_ON ejecución o ejecución y no en tiempo de análisis.
La configuración de SET ANSI_NULL_DFLT_ON no se aplica cuando se crean tablas usando la instrucción SELECT IN.
Para ver la configuración actual de este valor, ejecute la consulta siguiente.
DECLARE @ANSI_NULL_DFLT_ON VARCHAR(3) = 'OFF';
IF ( (1024 & @@OPTIONS) = 1024 ) SET @ANSI_NULL_DFLT_ON = 'ON';
SELECT @ANSI_NULL_DFLT_ON AS ANSI_NULL_DFLT_ON;
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 AdventureWorks2022;
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 AdventureWorks2022 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 AdventureWorks2022 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 AdventureWorks2022 SET ANSI_NULL_DEFAULT ON;
GO
-- Drop tables t1 through t6.
DROP TABLE t1,t2,t3,t4,t5,t6;
Consulte también
ALTER TABLE (Transact-SQL)
CREATE TABLE (Transact-SQL)
SET Instrucciones (Transact-SQL)
SET ANSI_DEFAULTS (Transact-SQL)
SET ANSI_NULL_DFLT_OFF (Transact-SQL)