Delen via


SET ANSI_NULL_DFLT_ON (Transact-SQL)

Van toepassing op:SQL ServerAzure SQL DatabaseAzure SQL Managed InstanceAzure Synapse AnalyticsAnalytics Platform System (PDW)SQL Analytics-eindpunt in Microsoft FabricMagazijn in Microsoft FabricSQL-database in Microsoft Fabric

Past het gedrag van de sessie aan om de standaardnulificatie van nieuwe kolommen te overschrijven wanneer de ANSI-nulstandaardoptie voor de database onjuist is. Voor meer informatie over het instellen van de waarde voor ANSI null default, zie ALTER DATABASE (Transact-SQL).

Transact-SQL syntaxis-conventies

Syntaxis

-- 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

Opmerkingen

Deze instelling beïnvloedt alleen de nullability van nieuwe kolommen wanneer de nullability van de kolom niet is gespecificeerd in de CREATE TABLE- en ALTER TABLE-instructies. Wanneer SET ANSI_NULL_DFLT_ON AAN is, staan nieuwe kolommen die worden aangemaakt met de ALTER TABLE- en CREATE TABLE-instructies nulwaarden toe als de nullability-status van de kolom niet expliciet is gespecificeerd. SET ANSI_NULL_DFLT_ON beïnvloedt kolommen die zijn aangemaakt met een expliciet NULL of NOT NULL niet.

Zowel SET ANSI_NULL_DFLT_OFF als SET ANSI_NULL_DFLT_ON kunnen niet tegelijkertijd AAN worden gezet. Als één optie AAN staat, staat de andere optie UIT. Daarom kunnen ANSI_NULL_DFLT_OFF of ANSI_NULL_DFLT_ON worden ingesteld AAN, of beide kunnen UIT worden gezet. Als een van beide opties AAN staat, treedt die instelling (SET ANSI_NULL_DFLT_OFF of SET ANSI_NULL_DFLT_ON) in werking. Als beide opties UIT staan, gebruikt SQL Server de waarde van de kolom is_ansi_null_default_on in de sys.databases catalogusweergave.

Voor een betrouwbaardere werking van Transact-SQL scripts die worden gebruikt in databases met verschillende nullability-instellingen, is het beter om NULL of NOT NULL te specificeren in de CREATE TABLE- en ALTER TABLE-instructies.

De SQL Server Native Client ODBC-driver en de SQL Server Native Client OLE DB Provider voor SQL Server zetten ANSI_NULL_DFLT_ON automatisch op AAN bij het verbinden. De standaard voor SET ANSI_NULL_DFLT_ON is UIT voor verbindingen van DB-Library applicaties.

Wanneer SET ANSI_DEFAULTS is ingeschakeld, is SET ANSI_NULL_DFLT_ON ingeschakeld.

De instelling van SET ANSI_NULL_DFLT_ON wordt ingesteld tijdens execute- of runtime en niet tijdens parsetijd.

De instelling van SET ANSI_NULL_DFLT_ON is niet van toepassing wanneer tabellen worden aangemaakt met de SELECT INTO-instructie.

Om de huidige instelling voor deze instelling te bekijken, voert u de volgende query uit.

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

Vereist lidmaatschap van de openbare rol.

Voorbeelden

Het volgende voorbeeld toont de effecten van SET ANSI_NULL_DFLT_ON met beide instellingen voor de ANSI null default database-optie.

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;  

Zie ook

ALTER TABLE (Transact-SQL)
MAAK TABEL AAN (Transact-SQL)
SET-instructies (Transact-SQL)
SET ANSI_DEFAULTS (Transact-SQL)
SET ANSI_NULL_DFLT_OFF (Transact-SQL)