Notitie
Voor toegang tot deze pagina is autorisatie vereist. U kunt proberen u aan te melden of de directory te wijzigen.
Voor toegang tot deze pagina is autorisatie vereist. U kunt proberen de mappen te wijzigen.
Van toepassing op:SQL Server
Azure SQL Database
Azure SQL Managed Instance
Azure Synapse Analytics
SQL Analytics-eindpunt in Microsoft Fabric
Magazijn in Microsoft Fabric
SQL-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
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 and-instructies ALTER TABLE . Standaard geldt, wanneer SETSET ANSI_NULL_DFLT_OFF AAN is, nieuwe kolommen die worden aangemaakt met de ALTER TABLE en-statements CREATE TABLE NIET NULL als de nullability-status van de kolom niet expliciet is gespecificeerd. SET ANSI_NULL_DFLT_OFF beïnvloedt kolommen die worden aangemaakt door expliciet NULL of NOT NULL te gebruiken.
Beide SETSET ANSI_NULL_DFLT_OFF en SETSET ANSI_NULL_DFLT_ON kunnen niet tegelijkertijd AAN worden gezet. Als één optie AAN staat, staat de andere optie UIT. Daarom kan een of ANSI_NULL_DFLT_OFFSETSET ANSI_NULL_DFLT_ON AAN worden gezet, of beide kunnen UIT worden gezet. Als een van beide opties AAN staat, treedt die instelling (SETSET ANSI_NULL_DFLT_OFF of SETSET 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 in CREATE TABLE en ALTER TABLE statements te specificeren.
De instelling van SET ANSI_NULL_DFLT_OFF wordt ingesteld bij uitvoering of uitvoeringstijd en niet op parseringstijd.
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.
Note
In Azure SQL Database en SQL Database in Microsoft Fabric is de USE instructie niet beschikbaar. Maak in plaats daarvan direct verbinding met de beoogde database en voer de rest van het codevoorbeeld uit.
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;