SET ANSI_NULL_DFLT_ON (Transact-SQL)

Modifica o comportamento da sessão para substituir a possibilidade de nulidade padrão de novas colunas quando a opção Padrão ANSI nulo do banco de dados for false. Para obter mais informações sobre como definir o valor de Padrão ANSI nulo, consulte ALTER DATABASE (Transact-SQL).

Aplica-se a: SQL Server (SQL Server 2008 até a versão atual), Banco de dados SQL do Windows Azure (versão inicial até a versão atual).

Ícone de vínculo de tópico Convenções da sintaxe Transact-SQL

Sintaxe

SET ANSI_NULL_DFLT_ON {ON | OFF}

Comentários

Esta configuração somente afetará a capacidade de nulidade de novas colunas quando a capacidade de nulidade da coluna não estiver especificada nas instruções CREATE TABLE e ALTER TABLE. Quando SET ANSI_NULL_DFLT_ON for ON, novas colunas criadas com o uso das instruções ALTER TABLE e CREATE TABLE permitirão valores nulos se o status da capacidade de nulidade da coluna não for especificado explicitamente. SET ANSI_NULL_DFLT_ON não afeta colunas criadas com um NULL ou NOT NULL explícito.

SET ANSI_NULL_DFLT_OFF e SET ANSI_NULL_DFLT_ON não podem ser definidos como ON ao mesmo tempo. Se uma opção for definida como ON, a outra será definida como OFF. Portanto, ANSI_NULL_DFLT_OFF ou ANSI_NULL_DFLT_ON pode ser definido como ON ou ambos podem ser definidos como OFF. Se uma das opções for ON, essa configuração (SET ANSI_NULL_DFLT_OFF ou SET ANSI_NULL_DFLT_ON) entrará em vigor. Se ambas as opções forem definidas como OFF, o SQL Server usará o valor da coluna is_ansi_null_default_on na exibição de catálogo sys.databases.

Para uma operação mais confiável dos scripts Transact-SQL usados em bancos de dados com diferentes configurações de capacidade de nulidade, é melhor especificar NULL ou NOT NULL nas instruções CREATE TABLE e ALTER TABLE.

O driver SQL Server Native Client ODBC e o SQL Server Native Client OLE DB Provider for SQL Server definem automaticamente ANSI_NULL_DFLT_ON como ON ao conectar. O padrão para SET ANSI_NULL_DFLT_ON é OFF para conexões de aplicativos DB-Library.

Quando SET ANSI_DEFAULTS for ON, SET ANSI_NULL_DFLT_ON está habilitado.

A configuração de SET ANSI_NULL_DFLT_ON é definida no momento da execução e não no momento da análise.

A configuração de SET ANSI_NULL_DFLT_ON não se aplica quando as tabelas são criadas usando a instrução SELECT INTO.

Para exibir a configuração atual dessa configuração, execute a consulta a seguir.

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;

Permissões

Requer associação à função pública.

Exemplos

O exemplo a seguir mostra os efeitos de SET ANSI_NULL_DFLT_ON com ambas as configurações da opção de banco de dados Padrão ANSI nulo.

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

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

Consulte também

Referência

ALTER TABLE (Transact-SQL)

CREATE TABLE (SQL Server)

Instruções SET (Transact-SQL)

SET ANSI_DEFAULTS (Transact-SQL)

SET ANSI_NULL_DFLT_OFF (Transact-SQL)