SET ANSI_NULL_DFLT_OFF (Transact-SQL)

Gilt für:SQL ServerAzure SQL-DatenbankVerwaltete Azure SQL-InstanzAzure Synapse AnalyticsAnalytics Platform System (PDW)SQL-Analyseendpunkt in Microsoft FabricLagerhaus in Microsoft FabricSQL-Datenbank in Microsoft Fabric

Ändert das Sitzungsverhalten, sodass die Standardeinstellung der NULL-Zulässigkeit für neue Spalten überschrieben wird, wenn die Option ANSI NULL Default für die Datenbank auf TRUE festgelegt ist. Weitere Informationen zur Einstellung des ANSI-Null-Standardwerts finden Sie unterALTER DATABASE (Transact-SQL).

Transact-SQL-Syntaxkonventionen

Syntax

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

Hinweise

Diese Einstellung wirkt sich nur auf die Nullfähigkeit neuer Spalten aus, wenn die Nullfähigkeit der Spalte in den CREATE TABLE and-Anweisungen ALTER TABLE nicht angegeben ist. Standardmäßig sind neue Spalten, die SETSET ANSI_NULL_DFLT_OFF mit den ALTER TABLE und-Anweisungen CREATE TABLE erstellt werden, wenn AN ist, NICHT NULL, wenn der Nullability-Status der Spalte nicht explizit angegeben ist. SET ANSI_NULL_DFLT_OFF betrifft keine Spalten, die durch explizite NULL oder NOT NULL erstellt werden.

Beide SETSET ANSI_NULL_DFLT_OFF und SETSET ANSI_NULL_DFLT_ON können nicht gleichzeitig EINGESCHALTET werden. Wird eine der beiden Optionen aktiviert (ON), so wird die andere deaktiviert (OFF). Daher kann entweder ANSI_NULL_DFLT_OFF oder SETSET ANSI_NULL_DFLT_ON AN gesetzt werden, oder beide können AUS gesetzt werden. Wenn eine der beiden Optionen AKTIVIERT ist, greift diese Einstellung (SETSET ANSI_NULL_DFLT_OFF oder SETSET ANSI_NULL_DFLT_ON). Wird für beide Optionen OFF festgelegt, verwendet SQL Server den Wert der Spalte is_ansi_null_default_on in der Katalogsicht sys.databases.

Für einen zuverlässigeren Betrieb Transact-SQL Skripte, die in Datenbanken mit unterschiedlichen Nullabilitätseinstellungen verwendet werden, ist es besser, immer NULL oder NOT NULL in CREATE TABLE und ALTER TABLE Anweisungen anzugeben.

Die Einstellung von SET ANSI_NULL_DFLT_OFF wird zur Ausführung oder Laufzeit und nicht zur Analysezeit festgelegt.

Um die aktuelle Einstellung anzuzeigen, führen Sie die folgende Abfrage aus.

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;  

Berechtigungen

Erfordert die Mitgliedschaft in der public-Rolle.

Beispiele

Im folgenden Beispiel werden die Auswirkungen der beiden Einstellungen von SET ANSI_NULL_DFLT_OFF für die Datenbankoption ANSI NULL Default gezeigt.

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;  
  

Weitere Informationen

ALTER TABLE (Transact-SQL)
CREATE TABLE (Transact-SQL)
SET Anweisungen (Transact-SQL)
SET ANSI_NULL_DFLT_ON (Transact-SQL)