SET ANSI_NULL_DFLT_ON (Transact-SQL)

Viene modificato il comportamento della sessione per eseguire l'override dell'impostazione predefinita che consente l'utilizzo dei valori Null di nuove colonne, quando l'opzione ANSI null default per il database è false. Per ulteriori informazioni sull'impostazione del valore per ANSI null default, vedere ALTER DATABASE (Transact-SQL) e Impostazione delle opzioni di database.

Icona di collegamento a un argomentoConvenzioni della sintassi Transact-SQL

Sintassi

SET ANSI_NULL_DFLT_ON {ON | OFF}

Osservazioni

Questa impostazione ha effetto solo sul supporto di valori Null per le nuove colonne quando tale supporto non è specificato nelle istruzioni CREATE TABLE e ALTER TABLE. Quando l'opzione SET ANSI_NULL_DFLT_ON è impostata su ON, le nuove colonne create con le istruzioni ALTER TABLE e CREATE TABLE supportano i valori Null se il supporto di valori Null per la colonna non è stato specificato in modo esplicito. SET ANSI_NULL_DFLT_OFF non ha effetto sulle colonne create tramite un NULL o un NOT NULL esplicito.

Non è possibile impostare contemporaneamente su ON entrambe le opzioni SET ANSI_NULL_DFLT_OFF e SET ANSI_NULL_DFLT_ON. Se un'opzione è impostata su ON, l'altra deve essere impostata su OFF. In altri termini, è possibile impostare su ON una delle due opzioni oppure impostare entrambe le opzioni su OFF. Se si imposta su ON una delle due opzioni SET ANSI_NULL_DFLT_OFF e SET ANSI_NULL_DFLT_ON, tale opzione risulta attivata. Se entrambe le opzioni sono impostate su OFF, in SQL Server viene utilizzato il valore della colonna is_ansi_null_default_on nella vista del catalogo sys.databases.

Per ottenere un'affidabilità maggiore dell'operazione degli script Transact-SQL utilizzabili in database con impostazioni per il supporto di valori Null diverse, nelle istruzioni CREATE TABLE e ALTER TABLE è consigliabile specificare sempre NULL o NOT NULL.

Tramite il driver ODBC di SQL Server Native Client e il provider OLE DB SQL Server Native Client per SQL Server l'opzione ANSI_NULL_DFLT_ON viene impostata automaticamente su ON al momento della connessione. L'opzione predefinita per SET ANSI_NULL_DFLT_ON è OFF per le connessioni di applicazioni DB-Library.

Quando l'opzione SET ANSI_DEFAULTS è impostata su ON, l'opzione SET ANSI_NULL_DFLT_ON risulta abilitata.

L'opzione SET ANSI_NULL_DFLT_ON viene impostata in fase di esecuzione, non in fase di analisi.

L'impostazione di SET ANSI_NULL_DFLT_ON non viene applicata quando le tabelle vengono create utilizzando l'istruzione SELECT INTO.

Autorizzazioni

È richiesta l'appartenenza al ruolo public.

Esempi

Nell'esempio seguente vengono illustrati gli effetti di SET ANSI_NULL_DFLT_ON con entrambe le impostazioni per l'opzione di database ANSI null default.

USE AdventureWorks2008R2;
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 AdventureWorks2008R2 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 AdventureWorks2008R2 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 AdventureWorks2008R2 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;