Nota
L'accesso a questa pagina richiede l'autorizzazione. È possibile provare ad accedere o modificare le directory.
L'accesso a questa pagina richiede l'autorizzazione. È possibile provare a modificare le directory.
Si applica a:SQL Server
Database SQL di Azure
Istanza gestita di SQL di Azure
Azure Synapse Analytics
Piattaforma di analisi (PDW)
Endpoint di analisi SQL in Microsoft Fabric
Magazzino in Microsoft Fabric
Database SQL in Microsoft Fabric
Modifica il comportamento della sessione in modo da eseguire l'override dell'impostazione predefinita relativa al supporto di valori Null delle nuove colonne, quando l'opzione di database ANSI null default è false. Per maggiori informazioni sull'impostazione del valore per ANSI null default, vedi ALTER DATABASE (Transact-SQL).
Convenzioni relative alla sintassi Transact-SQL
Sintassi
-- 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
Osservazioni:
Questa impostazione influisce sulla nullabilità delle nuove colonne solo quando la nullabilità della colonna non è specificata nelle CREATE TABLE istruzioni e.ALTER TABLE Quando SETSET ANSI_NULL_DFLT_ON è ON, nuove colonne create usando le ALTER TABLE istruzioni e CREATE TABLE permettono valori nulli se lo stato di nullabilità della colonna non è esplicitamente specificato. SET ANSI_NULL_DFLT_ON non influisce sulle colonne create con un esplicito NULL o NOT NULL.
Entrambi SETSET ANSI_NULL_DFLT_OFF e SETSET ANSI_NULL_DFLT_ON non possono essere impostati SU contemporaneamente. Se un'opzione è impostata su ON, l'altra deve essere impostata su OFF. Pertanto, o ANSI_NULL_DFLT_OFF può ANSI_NULL_DFLT_ON essere impostato ON, o entrambi possono essere attivati. Se una delle due opzioni è ATTIVA, quell'impostazione (SETSET ANSI_NULL_DFLT_OFF o SETSET ANSI_NULL_DFLT_ON) entra in vigore. Se entrambe le opzioni sono impostate su OFF, SQL Server usa il valore della colonna is_ansi_null_default_on nella vista di catalogo sys.databases.
Per un funzionamento più affidabile degli script Transact-SQL utilizzati in database con diverse impostazioni di nullability, è meglio specificare le istruzioni NULL o NOT NULL in CREATE TABLE e ALTER TABLE
Il driver ODBC di SQL Server Native Client e il provider OLE DB di SQL Server Native Client per SQL Server vengono impostati automaticamente su ANSI_NULL_DFLT_ON ON durante la connessione. Il valore predefinito per SET ANSI_NULL_DFLT_ON OFF per le connessioni da DB-Library applicazioni.
Quando SETSET ANSI_DEFAULTS è ATTIVATO, SETSET ANSI_NULL_DFLT_ON è abilitato.
L'impostazione di viene impostata in fase di SET ANSI_NULL_DFLT_ON esecuzione o in fase di esecuzione e non in fase di analisi.
L'impostazione di SET ANSI_NULL_DFLT_ON non si applica quando le tabelle vengono create utilizzando l'istruzione SELECT IN.
Per visualizzare l'impostazione corrente per questa impostazione, eseguire la query riportata di seguito.
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;
Autorizzazioni
È richiesta l'appartenenza al ruolo public .
Esempi
In questo esempio vengono illustrati gli effetti dell'opzione SET ANSI_NULL_DFLT_ON con entrambe le impostazioni dell'opzione di database ANSI null default.
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;
Vedi anche
ALTER TABLE (Transact-SQL)
CREATE TABLE (Transact-SQL)
SET Istruzioni (Transact-SQL)
SET ANSI_DEFAULTS (Transact-SQL)
SET ANSI_NULL_DFLT_OFF (Transact-SQL)