當(dāng)數(shù)據(jù)庫的 ANSI null default 選項是 true 時,更改會話行為以替代新列的默認為空性。有關(guān)設(shè)置 ANSI null default 值的更多信息,請參見 sp_dboption 和設(shè)置數(shù)據(jù)庫選項。
SET ANSI_NULL_DFLT_OFF {ON | OFF}
當(dāng)在 CREATE TABLE 和 ALTER TABLE 語句中沒有指定列的為空性時,該設(shè)置僅影響新列的為空性。當(dāng) SET ANSI_NULL_DFLT_OFF 為 ON 時,如果沒有顯式指定列的為空性狀態(tài),默認情況下,使用 ALTER TABLE 和 CREATE TABLE 語句創(chuàng)建的新列將為 NOT NULL。SET ANSI_NULL_DFLT_OFF 對使用顯式 NULL 或 NOT NULL 創(chuàng)建的列無效。
不能將 SET ANSI_NULL_DFLT_OFF 和 SET ANSI_NULL_DFLT_ON 同時設(shè)置為 ON。如果將一個選項設(shè)置為 ON,則將另一個選項設(shè)置為 OFF。因此,可以或者將 ANSI_NULL_DFLT_OFF 或 SET ANSI_NULL_DFLT_ON 設(shè)置為 ON,或者將二者都設(shè)置為 OFF。如果有一個選項為 ON,則該選項(SET ANSI_NULL_DFLT_OFF 或 SET ANSI_NULL_DFLT_ON)生效。如果兩個選項都設(shè)置為 OFF,則 Microsoft® SQL Server™ 將使用 sp_dboption 的 ANSI null default 選項值。
為使 Transact-SQL 腳本在具有不同為空性設(shè)置的數(shù)據(jù)庫中獲得最可靠的操作,最好始終在 CREATE TABLE 和 ALTER TABLE 語句中指定 NULL 或 NOT NULL。
SET ANSI_NULL_DFLT_OFF 的設(shè)置是在執(zhí)行或運行時設(shè)置,而不是在分析時設(shè)置。
SET ANSI_NULL_DFLT_OFF 權(quán)限默認授予所有用戶。
下例顯示 SET ANSI_NULL_DFLT_OFF 使用 ANSI null default 數(shù)據(jù)庫選項的兩個設(shè)置時的效果。
USE pubs
GO
-- Set the 'ANSI null default' database option to true by executing
-- sp_dboption.
GO
EXEC sp_dboption 'pubs','ANSI null default','true'
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 sp_dboption and SET
-- option disabled.
-- Set the 'ANSI null default' database option to false.
EXEC sp_dboption 'pubs','ANSI null default','false'
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
DROP TABLE t2
DROP TABLE t3
DROP TABLE t4
DROP TABLE t5
DROP TABLE t6
GO
相關(guān)文章