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
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
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 and-instructies ALTER TABLE . Wanneer SETSET ANSI_NULL_DFLT_ON AAN is, staan nieuwe kolommen die worden aangemaakt met de ALTER TABLE and-instructies CREATE TABLE nulwaarden toe als de nullability-status van de kolom niet expliciet is gespecificeerd. SET ANSI_NULL_DFLT_ON beïnvloedt niet kolommen die zijn aangemaakt met een expliciet NULL of NOT NULL.
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_OFFANSI_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 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 in CREATE TABLE en ALTER TABLE statements te specificeren.
Het ODBC-stuurprogramma van sql Server Native Client en de OLE DB-provider van SQL Server voor SQL Server worden automatisch ingesteld ANSI_NULL_DFLT_ON op AAN wanneer er verbinding wordt gemaakt. De standaard voor SET ANSI_NULL_DFLT_ON is UIT voor verbindingen van DB-Library applicaties.
Wanneer SETSET ANSI_DEFAULTS is INGESCHAKELD, SETSET ANSI_NULL_DFLT_ON is ingeschakeld.
De instelling van SET ANSI_NULL_DFLT_ON wordt ingesteld bij uitvoering of uitvoeringstijd en niet op parseringstijd.
De instelling van SET ANSI_NULL_DFLT_ON geldt niet 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.
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
-- 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;