SET ANSI_NULL_DFLT_ON (Transact-SQL)

适用于:SQL ServerAzure SQL 数据库Azure SQL 托管实例Azure Synapse AnalyticsMicrosoft Fabric中的SQL分析端点Microsoft Fabric中的仓库Microsoft Fabric中的SQL数据库

当数据库的 ANSI null default 选项为 false 时,修改会话的行为以覆盖新列的默认为 Null 性。 有关设置 ANSI空默认值的更多信息,请参见 ALTER DATABASE (Transact-SQL)。

Transact-SQL 语法约定

语法

-- Syntax for SQL Server and Azure SQL Database and Microsoft Fabric

SET ANSI_NULL_DFLT_ON {ON | OFF}
-- Syntax for Azure Synapse Analytics

SET ANSI_NULL_DFLT_ON ON

备注

该设置仅影响当列的可空性未在 CREATE TABLE and ALTER TABLE 语句中指定时,新列的可空性。 当 SETSET ANSI_NULL_DFLT_ON 是 ON,使用 ALTER TABLE 和 CREATE TABLE 语句创建的新列如果未明确指定该列的空值状态,则允许为空值。 SET ANSI_NULL_DFLT_ON 不影响带有显式NULL或非NULL的列。

SET SET ANSI_NULL_DFLT_OFF SET SET ANSI_NULL_DFLT_ON两个和不能同时开启。 如果将一个选项设置为 ON,则将另一个选项设置为 OFF。 因此,任一 ANSI_NULL_DFLT_OFF 或 ANSI_NULL_DFLT_ON 都可以设置为开启,或者两者都可以设置为关闭。 如果任一选项开启,该设置SETSET ANSI_NULL_DFLT_OFF (或 SETSET ANSI_NULL_DFLT_ON)生效。 如果将这两个选项都设置为 OFF,则 SQL Server 将使用 sys.databases 目录视图中 is_ansi_null_default_on 列的值。

为了更可靠的 Transact-SQL 脚本操作,适用于具有不同空值设置的数据库中的脚本,最好在和CREATE TABLE语句中指定NULL或NOT NULLALTER TABLE。

连接时,SQL Server Native Client ODBC 驱动程序和 SQL Server Native Client OLE DB Provider for SQL Server 会自动设置为 ANSI_NULL_DFLT_ON ON。 对于来自 DB-Library 应用程序的连接,默认 SET ANSI_NULL_DFLT_ON 是关闭。

启用启用时间SETSET ANSI_DEFAULTS。 SETSET ANSI_NULL_DFLT_ON

SET ANSI_NULL_DFLT_ON设置是在执行或运行时设置的,而不是在分析时设置的。

当使用 SELECT INTO 语句创建表时,设置 SET ANSI_NULL_DFLT_ON 不适用。

要查看此设置的当前设置,请运行以下查询。

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;  
  

权限

要求 公共 角色具有成员身份。

示例

以下示例显示当 SET ANSI_NULL_DFLT_ON 数据库选项在两种设置下时,对 的影响。

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;