當(dāng)數(shù)據(jù)庫(kù)的 ANSI null default選項(xiàng)為 false 時(shí),更改會(huì)話行為以替代新列的默認(rèn)為空性。有關(guān)設(shè)置 ANSI null default 值的更多信息,請(qǐng)參見 sp_dboption 和設(shè)置數(shù)據(jù)庫(kù)選項(xiàng)。
SET ANSI_NULL_DFLT_ON {ON | OFF}
當(dāng)在 CREATE TABLE 和 ALTER TABLE 語句中沒有指定列的為空性時(shí),該設(shè)置僅影響新列的為空性。當(dāng) SET ANSI_NULL_DFLT_ON 為 ON 時(shí),如果沒有顯式指定列的為空性狀態(tài),則使用 ALTER TABLE 和 CREATE TABLE 語句創(chuàng)建的新列可以使用空值。SET ANSI_NULL_DFLT_ON 對(duì)使用顯式 NULL 或 NOT NULL 創(chuàng)建的列無效。
不能將 SET ANSI_NULL_DFLT_OFF 和 SET ANSI_NULL_DFLT_ON 同時(shí)設(shè)置為 ON。如果將一個(gè)選項(xiàng)設(shè)置為 ON,則將另一個(gè)選項(xiàng)設(shè)置為 OFF。因此,可以或者將 ANSI_NULL_DFLT_OFF 或 ANSI_NULL_DFLT_ON 設(shè)置為 ON,或者將二者都設(shè)置為 OFF。如果有一個(gè)選項(xiàng)為 ON,則該選項(xiàng)(SET ANSI_NULL_DFLT_OFF 或 SET ANSI_NULL_DFLT_ON)生效。如果兩個(gè)選項(xiàng)都設(shè)置為 OFF,則 Microsoft® SQL Server™ 將使用 sp_dboption 的 ANSI null default 選項(xiàng)值。
為使 Transact-SQL 腳本在含有不同為空性設(shè)置的數(shù)據(jù)庫(kù)中獲得最可靠的操作,最好始終在 CREATE TABLE 和 ALTER TABLE 語句中指定 NULL 或 NOT NULL。
SQL Server ODBC 驅(qū)動(dòng)程序和用于 SQL Server 的 Microsoft OLE DB 提供程序在連接時(shí)自動(dòng)將 ANSI_NULL_DFLT_ON 設(shè)置為 ON。對(duì)來自 DB-Library 應(yīng)用程序的連接,SET ANSI_NULL_DFLT_ON 默認(rèn)為 OFF。
當(dāng) SET ANSI_DEFAULTS 為 ON 時(shí),將啟用 SET ANSI_NULL_DFLT_ON。
SET ANSI_NULL_DFLT_ON 的設(shè)置是在執(zhí)行或運(yùn)行時(shí)設(shè)置,而不是在分析時(shí)設(shè)置。
SET ANSI_NULL_DFLT_ON 權(quán)限默認(rèn)授予所有用戶。
下例顯示 SET ANSI_NULL_DFLT_ON 使用 ANSI null default 數(shù)據(jù)庫(kù)選項(xiàng)的兩個(gè)設(shè)置時(shí)的效果。
USE pubs
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
-- sp_dboption.
EXEC sp_dboption 'pubs','ANSI null default','false'
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.
EXEC sp_dboption 'pubs','ANSI null default','true'
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.
EXEC sp_dboption 'pubs','ANSI null default','false'
GO
-- Drop tables t1 through t6.
DROP TABLE t1
DROP TABLE t2
DROP TABLE t3
DROP TABLE t4
DROP TABLE t5
DROP TABLE t6
GO
相關(guān)文章