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 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 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_OFF { ON | OFF }
-- Syntax for Azure Synapse Analytics and Parallel Data Warehouse
SET ANSI_NULL_DFLT_OFF OFF
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 . Por defecto, cuando SETSET ANSI_NULL_DFLT_OFF está ACTIVADO, las nuevas columnas creadas usando las ALTER TABLE sentencias y CREATE TABLE NO son NULAS si el estado de nulidad de la columna no está especificado explícitamente. SET ANSI_NULL_DFLT_OFF no afecta a las columnas creadas usando 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 SETSET 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 siempre NULL o NOT NULL en CREATE TABLE las sentencias and ALTER TABLE .
El valor de se establece en tiempo de SET ANSI_NULL_DFLT_OFF ejecución o ejecución y no en tiempo de análisis.
Para ver la configuración actual de este valor, ejecute la consulta siguiente.
DECLARE @ANSI_NULL_DFLT_OFF VARCHAR(3) = 'OFF';
IF ( (2048 & @@OPTIONS) = 2048 ) SET @ANSI_NULL_DFLT_OFF = 'ON';
SELECT @ANSI_NULL_DFLT_OFF AS ANSI_NULL_DFLT_OFF;
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 AdventureWorks2022;
GO
-- Set the 'ANSI null default' database option to true by executing
-- ALTER DATABASE.
GO
ALTER DATABASE AdventureWorks2022 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 AdventureWorks2022 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;
Consulte también
ALTER TABLE (Transact-SQL)
CREATE TABLE (Transact-SQL)
SET Instrucciones (Transact-SQL)
SET ANSI_NULL_DFLT_ON (Transact-SQL)