Delen via


SET ANSI_NULL_DFLT_OFF (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

Verandert het gedrag van de sessie om de standaardnulbaarheid van nieuwe kolommen te overrulen wanneer de ANSI-nulstandaardoptie voor de database waar 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_OFF { ON | OFF }
-- Syntax for Azure Synapse Analytics and Parallel Data Warehouse

SET ANSI_NULL_DFLT_OFF OFF

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. Standaard, wanneer SET ANSI_NULL_DFLT_OFF AAN is, zijn nieuwe kolommen die worden aangemaakt met de ALTER TABLE- en CREATE TABLE-statements NIET NULL als de nulbaarheidsstatus van de kolom niet expliciet is gespecificeerd. SET ANSI_NULL_DFLT_OFF beïnvloedt geen kolommen die worden aangemaakt door expliciet NULL of NOT NULL te gebruiken.

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 zowel ANSI_NULL_DFLT_OFF als SET ANSI_NULL_DFLT_ON AAN worden gezet, 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 het sys.databases-catalogusbeeld .

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

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

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

DECLARE @ANSI_NULL_DFLT_OFF VARCHAR(3) = 'OFF';  
IF ( (2048 & @@OPTIONS) = 2048 ) SET @ANSI_NULL_DFLT_OFF = 'ON';  
SELECT @ANSI_NULL_DFLT_OFF AS ANSI_NULL_DFLT_OFF;  

Permissions

Vereist lidmaatschap van de openbare rol.

Voorbeelden

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

USE AdventureWorks2022;  
GO  
  
-- Set the 'ANSI null default' database option to true by executing   
-- ALTER DATABASE.  
GO  
ALTER DATABASE AdventureWorks2022 SET ANSI_NULL_DEFAULT ON;  
GO  
-- Create table t1.  
CREATE TABLE t1 (a TINYINT);  
GO  
-- NULL INSERT should succeed.  
INSERT INTO t1 (a) VALUES (NULL);  
GO  
  
-- SET ANSI_NULL_DFLT_OFF to ON and create table t2.  
SET ANSI_NULL_DFLT_OFF ON;  
GO  
CREATE TABLE t2 (a TINYINT);  
GO   
-- NULL INSERT should fail.  
INSERT INTO t2 (a) VALUES (NULL);  
GO  
  
-- SET ANSI_NULL_DFLT_OFF to OFF and create table t3.  
SET ANSI_NULL_DFLT_OFF OFF;  
GO  
CREATE TABLE t3 (a TINYINT) ;  
GO   
-- NULL INSERT should succeed.  
INSERT INTO t3 (a) VALUES (NULL);  
GO  
  
-- This illustrates the effect of having both the database  
-- option and SET option disabled.  
-- Set the 'ANSI null default' database option to false.  
ALTER DATABASE AdventureWorks2022 SET ANSI_NULL_DEFAULT OFF;  
GO  
-- Create table t4.  
CREATE TABLE t4 (a TINYINT) ;  
GO   
-- NULL INSERT should fail.  
INSERT INTO t4 (a) VALUES (NULL);  
GO  
  
-- SET ANSI_NULL_DFLT_OFF to ON and create table t5.  
SET ANSI_NULL_DFLT_OFF ON;  
GO  
CREATE TABLE t5 (a TINYINT);  
GO   
-- NULL insert should fail.  
INSERT INTO t5 (a) VALUES (NULL);  
GO  
  
-- SET ANSI_NULL_DFLT_OFF to OFF and create table t6.  
SET ANSI_NULL_DFLT_OFF OFF;  
GO  
CREATE TABLE t6 (a TINYINT);   
GO   
-- NULL insert should fail.  
INSERT INTO t6 (a) VALUES (NULL);  
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_NULL_DFLT_ON (Transact-SQL)