SET ANSI_NULL_DFLT_ON (Transact-SQL)

Aplica-se a:SQL ServerBanco de Dados SQL do AzureInstância Gerenciada de SQL do AzureAzure Synapse AnalyticsAnalytics Platform System (PDW)Ponto de extremidade de análise de SQL no Microsoft FabricDepósito no Microsoft FabricBanco de dados SQL no Microsoft Fabric

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 mais informações sobre como definir o valor para o padrão nulo ANSI, veja ALTER DATABASE (Transact-SQL).

Convenções de sintaxe de Transact-SQL

Sintaxe

-- Syntax for SQL Server and Azure SQL Database and Microsoft Fabric

SET ANSI_NULL_DFLT_ON {ON | OFF}
-- Syntax for Azure Synapse Analytics and Parallel Data Warehouse

SET ANSI_NULL_DFLT_ON ON

Comentários

Essa configuração só afeta a nulidade das novas colunas quando a nulidade da coluna não está especificada nas CREATE TABLE instruções e ALTER TABLE . Quando SETSET ANSI_NULL_DFLT_ON está ON, novas colunas criadas usando as ALTER TABLE instruções e CREATE TABLE permitem valores nulos se o status de nulidade da coluna não for explicitamente especificado. SET ANSI_NULL_DFLT_ON não afeta colunas criadas com um NULL explícito ou NOT NULL.

Tanto SETSET ANSI_NULL_DFLT_OFF quanto SETSET ANSI_NULL_DFLT_ON LIGADO ao mesmo tempo. Se uma opção for definida como ON, a outra será definida como OFF. Portanto, ou ANSI_NULL_DFLT_OFF ou ANSI_NULL_DFLT_ON pode ser ativado, ou ambos podem ser desligados. Se qualquer uma das opções estiver ATIVADA, essa configuração (SETSET ANSI_NULL_DFLT_OFF ou SETSET ANSI_NULL_DFLT_ON) entra 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 anulabilidade, é melhor especificar NULL ou NOT NULL em CREATE TABLE as instruções and ALTER TABLE .

O driver ODBC do SQL Server Native Client e o Provedor OLE DB do SQL Server Native Client para SQL Server são definidos ANSI_NULL_DFLT_ON automaticamente como ON ao se conectar. O padrão para SET ANSI_NULL_DFLT_ON é DESLIGADO para conexões de DB-Library aplicações.

Quando SETSET ANSI_DEFAULTS está ON, SETSET ANSI_NULL_DFLT_ON está habilitado.

A configuração de é definida em tempo de execução ou execução e não em tempo de SET ANSI_NULL_DFLT_ON 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 INS.

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 para a opção de banco de dados Padrão ANSI nulo.

USE AdventureWorks2022;  
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 AdventureWorks2022 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 AdventureWorks2022 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 AdventureWorks2022 SET ANSI_NULL_DEFAULT ON;  
GO  
  
-- Drop tables t1 through t6.  
DROP TABLE t1,t2,t3,t4,t5,t6;  

Consulte Também

ALTER TABLE (Transact-SQL)
CREATE TABLE (Transact-SQL)
SET Instruções (Transact-SQL)
SET ANSI_DEFAULTS (Transact-SQL)
SET ANSI_NULL_DFLT_OFF (Transact-SQL)