SET ANSI_NULL_DFLT_ON (Transact-SQL)

當資料庫的 ANSI null default 選項是 false 時,修改工作階段的行為來覆寫新資料行的預設 Null 屬性。如需有關設定 ANSI null default 這個值的詳細資訊,請參閱<ALTER DATABASE (Transact-SQL)>和<設定資料庫選項>。

主題連結圖示Transact-SQL 語法慣例

語法

SET ANSI_NULL_DFLT_ON {ON | OFF}

備註

這項設定只在 CREATE TABLE 和 ALTER TABLE 陳述式未指定新資料行的 Null 屬性時,才會影響新資料行的 Null 屬性設定。當 SET ANSI_NULL_DFLT_ON 是 ON 時,如果未明確指定資料行的 Null 屬性狀態,ALTER TABLE 和 CREATE TABLE 陳述式所建立的新資料行會接受 Null 值。SET ANSI_NULL_DFLT_ON 不會影響利用明確的 NULL 或 NOT NULL 來建立的資料行。

SET ANSI_NULL_DFLT_OFF 和 SET ANSI_NULL_DFLT_ON 不能同時設為 ON。如果一個選項設為 ON,另一個選項便設為 OFF。因此,您可以將 ANSI_NULL_DFLT_OFF 或 ANSI_NULL_DFLT_ON 設為 ON,也可以同時將它們設為 OFF。如果任何一個選項是 ON,這項設定 (SET ANSI_NULL_DFLT_OFF 或 SET ANSI_NULL_DFLT_ON) 就會生效。如果兩個選項都設為 OFF,SQL Server 會使用 sys.databases 目錄檢視中之 is_ansi_null_default_on 資料行的值。

如果希望在資料庫中搭配不同 Null 屬性設定來使用的 Transact-SQL 指令碼作業比較可靠,最好是在 CREATE TABLE 和 ALTER TABLE 陳述式中指定 NULL 或 NOT NULL。

SQL Server 的 SQL Server Native Client ODBC 驅動程式和 SQL Server Native Client OLE DB 提供者在連接之時,都會將 ANSI_NULL_DFLT_ON 設為 ON。起始於 DB-Library 應用程式的連接之 SET ANSI_NULL_DFLT_ON 預設值是 OFF。

當 SET ANSI_DEFAULTS 是 ON 時,會啟用 SET ANSI_NULL_DFLT_ON。

SET ANSI_NULL_DFLT_ON 的設定是在執行階段進行設定,而不是在剖析階段進行設定。

權限

需要 public 角色中的成員資格。

範例

下列範例會顯示含有兩種 ANSI null default 資料庫選項設定之 SET ANSI_NULL_DFLT_ON 的效果。

USE AdventureWorks;
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 AdventureWorks 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 AdventureWorks 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 AdventureWorks SET ANSI_NULL_DEFAULT ON
GO

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