Partilhar via


SET ANSI_NULL_DFLT_ON (Transact-SQL)

Aplica-se a:SQL ServerBase de Dados SQL do AzureInstância Gerida do Azure SQLAzure Synapse AnalyticsSistema de Plataforma de Análise (PDW)Ponto de Extremidade de Análise SQL no Microsoft FabricArmazém no Microsoft FabricBase de Dados SQL no Microsoft Fabric

Modifica o comportamento da sessão para sobrepor a nulidade padrão das novas colunas quando a opção ANSI nula para a base de dados é falsa. Para mais informações sobre a definição do valor para o padrão nulo ANSI, consulte ALTER DATABASE (Transact-SQL).

Transact-SQL convenções de sintaxe

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

Observações

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

Tanto SET ANSI_NULL_DFLT_OFF como SET ANSI_NULL_DFLT_ON não podem ser ativados ao mesmo tempo. Se uma opção estiver ATIVADA, a outra opção fica DESLIGADA. Portanto, tanto ANSI_NULL_DFLT_OFF como ANSI_NULL_DFLT_ON podem ser ativados, ou ambos podem ser desativados. Se alguma das opções estiver ATIVADA, essa definição (SET ANSI_NULL_DFLT_OFF ou SET ANSI_NULL_DFLT_ON) entra em vigor. Se ambas as opções estiverem desativadas, o SQL Server utiliza o valor da coluna is_ansi_null_default_on na vista de catálogo sys.databases .

Para uma operação mais fiável dos scripts Transact-SQL usados em bases de dados com diferentes definições de anulabilidade, é 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 para SQL Server definem automaticamente ANSI_NULL_DFLT_ON como ON ao ligar. O padrão para SET ANSI_NULL_DFLT_ON é DESLIGADO para ligações de DB-Library aplicações.

Quando SET ANSI_DEFAULTS está ON, SET ANSI_NULL_DFLT_ON está ativado.

A definição do SET ANSI_NULL_DFLT_ON é definida em tempo de execução ou execução e não em tempo de análise sintética.

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

Para visualizar a definição atual desta definição, execute a seguinte consulta.

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;  
  

Permissions

Requer adesão à função pública de .

Examples

O exemplo seguinte mostra os efeitos de SET ANSI_NULL_DFLT_ON com ambas as definições para a opção de base de dados padrão ANSI .

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;  

Ver também

ALTERAR TABELA (Transact-SQL)
CRIAR TABELA (Transact-SQL)
Instruções SET (Transact-SQL)
CONJUNTO ANSI_DEFAULTS (Transact-SQL)
SET ANSI_NULL_DFLT_OFF (Transact-SQL)