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, vejaALTER 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 CREATE TABLE instruções e ALTER TABLE . Quando SETSET ANSI_NULL_DFLT_ON está ON, novas colunas criadas usando ALTER TABLE as instruções e CREATE TABLE permitem valores nulos se o estado de anulabilidade da coluna não for explicitamente especificado. SET ANSI_NULL_DFLT_ON não afeta colunas criadas com um NULL ou NOT NULL explícito.

Ambos SETSET ANSI_NULL_DFLT_OFF e SETSET 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, ou ANSI_NULL_DFLT_OFFANSI_NULL_DFLT_ON pode ser definido ON, ou ambos podem ser desligados. Se qualquer uma das opções estiver ATIVADA, essa definição (SETSET ANSI_NULL_DFLT_OFF ou SETSET 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 CREATE TABLE 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 ANSI_NULL_DFLT_ON definidos automaticamente como ON ao se conectar. O padrão para SET ANSI_NULL_DFLT_ON é DESLIGADO para ligações de DB-Library aplicações.

Quando SETSET ANSI_DEFAULTS está LIGADO, SETSET ANSI_NULL_DFLT_ON está ativado.

A definição de 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

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