SET SET ARITHABORT (Transact-SQL)

Si applica a:SQL ServerDatabase SQL di AzureIstanza gestita di SQL di AzureAzure Synapse AnalyticsPiattaforma di analisi (PDW)Endpoint di analisi SQL in Microsoft FabricMagazzino in Microsoft FabricDatabase SQL in Microsoft Fabric

L'impostazione SET ARITHABORT determina se una query si interrompe quando si verifica un errore di overflow o di divisione per zero durante l'esecuzione della query.

  • Quando si imposta SET ARITHABORT ON e SET ANSI_WARNINGS OFF, errori aritmetici fanno terminare il batch. Se gli errori si verificano in una transazione, viene eseguito il rollback della transazione.

  • Se sia e sono sia che si verifica un errore aritmetico, appare un messaggio di avviso (a meno che SET ARITHIGNORE non sia ON) e il risultato dell'operazione aritmetica è NULL.OFFSET ANSI_WARNINGSSET ARITHABORT

Convenzioni relative alla sintassi Transact-SQL

Sintassi

Sintassi per SQL Server, server less SQL pool in Azure Synapse Analytics, Microsoft Fabric, database SQL in Microsoft Fabric


SET ARITHABORT { ON | OFF }

Sintassi per Azure Synapse Analytics e piattaforma di strumenti analitici (PDW)


SET ARITHABORT ON

Osservazioni:

Quando ANSI_WARNINGS è ON (il valore predefinito), l'impostazione di ARITHABORT non ha effetto funzionale. Errori aritmetici fanno terminare la query, ma il batch non si interrompe (finché l'impostazione XACT_ABORT è OFF).

Avviso

L'impostazione predefinita ARITHABORT per SQL Server Management Studio (SSMS) è ON, mentre una connessione client in un'applicazione è predefinita su ARITHABORT OFF. Anche se non c'è differenza funzionale finché ANSI_WARNINGS lo è ON, l'impostazione ARITHABORT resta comunque una chiave cache. Pertanto, SSMS e un'applicazione che utilizzano entrambi i rispettivi valori predefiniti hanno voci di cache diverse e possono avere piani di query differenti, rendendo difficile risolvere i problemi di query con scarse prestazioni. Cioè, la stessa query potrebbe essere eseguita più lentamente nell'applicazione rispetto a SSMS. Quando si risolvono query con Management Studio, abbina sempre le impostazioni del client ARITHABORT .

Per la valutazione delle espressioni, se sia SET ANSI_WARNINGS e SET ARITHABORT sono OFF sia una INSERT, UPDATE, o DELETE un'istruzione si imbattono in un errore aritmetico, di overflow, di divisione per zero o di dominio, la query inserisce o aggiorna un NULL valore. Se la colonna di destinazione non ammette valori Null, l'operazione di inserimento o aggiornamento ha esito negativo e viene generato un errore per l'utente.

L'impostazione di SET ARITHABORT avviene a tempo di esecuzione o di esecuzione e non a tempo di analisi.

SET ARITHABORT OFFnon è supportato nei pool SQL dedicati di Azure Synapse Analytics.

Autorizzazioni

È richiesta l'appartenenza al ruolo public .

Visualizza l'attuale ambientazione di ARITHABORT

Per visualizzare l'impostazione attuale per SET ARITHABORT, esegui la seguente query T-SQL:

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

Esempi

L'esempio seguente mostra gli errori di divisione per zero e di overflow con impostazioni diverse SET ARITHABORT .

  1. Lo script crea tabelle t1 di esempio e t2 inserisce valori di dati di esempio.
  2. Imposta ANSI_WARNINGS ON ed esegui test per vedere il comportamento predefinito di un errore di divisione per zero e di un overflow aritmetico.
  3. Imposta ANSI_WARNINGS OFF in modo che ARITHABORT abbia effetto, e imposta ARITHABORT ON. Esegui test per vedere il comportamento con errore di divisione per zero e overflow aritmetico.
  4. Imposta sia ANSI_WARNINGS OFF che ARITHABORT OFF. Esegui test per vedere il comportamento con errore di divisione per zero e overflow aritmetico.
  5. Sistemate le tabelle di esempio.
-- SET ARITHABORT  
-------------------------------------------------------------------------------  
-- Create tables t1 and t2 and insert data values.  
CREATE TABLE t1 (  
   a TINYINT,   
   b TINYINT  
);  
CREATE TABLE t2 (  
   a TINYINT NOT NULL  
);  
GO  
INSERT INTO t1   
VALUES (1, 0);  
INSERT INTO t1   
VALUES (255, 1);  
GO  

-- First run with ANSI_WARNINGS ON to see the default behavior.
PRINT '*** SET ANSI_WARNINGS ON';  
SET ANSI_WARNINGS ON;
SET XACT_ABORT OFF;    -- To make sure that we have the default setting for this option.  
GO
PRINT '*** Testing divide-by-zero during SELECT';  
GO  
SELECT a / b AS ab   
FROM t1;  
PRINT 'This prints, despite the error message.';
GO  

PRINT '*** Testing divide-by-zero during INSERT';  
GO  
INSERT INTO t2  
SELECT a / b AS ab    
FROM t1;  
PRINT 'This prints, despite the error message.';
GO  

PRINT '*** Testing tinyint overflow';  
GO  
INSERT INTO t2  
SELECT a + b AS ab   
FROM t1;  
PRINT 'This prints, despite the error message.';
GO  

PRINT '*** Resulting data - should be no data';  
GO  
SELECT *   
FROM t2;  
GO  

-- Truncate table t2.  
TRUNCATE TABLE t2;  
GO  

-- Set ANSI_WARNINGS OFF so that ARITHABORT has effect, and set ARITHABORT ON.
PRINT '*** SET ANSI_WARNINGS OFF; SET ARITHABORT ON';  
GO  
SET ANSI_WARNINGS OFF; 
SET ARITHABORT ON;  
GO  

PRINT '*** Testing divide-by-zero during SELECT';  
GO  
SELECT a / b AS ab    
FROM t1;  
PRINT 'This does not print.';
GO  

PRINT '*** Testing divide-by-zero during INSERT';  
GO  
INSERT INTO t2  
SELECT a / b AS ab    
FROM t1;  
PRINT 'This does not print.';
GO  
PRINT '*** Testing tinyint overflow';  
GO  
INSERT INTO t2  
SELECT a + b AS ab   
FROM t1;  
PRINT 'This does not print.';
GO  

PRINT '*** Resulting data - should be 0 rows';  
GO  
SELECT *   
FROM t2;  
GO  

-- Truncate table t2.  
TRUNCATE TABLE t2;  
GO  

-- Set both ANSI_WARNINGS OFF and ARITHABORT OFF.
PRINT '*** SET ARITHABORT OFF';  
GO  
SET ARITHABORT OFF;  
GO  

PRINT '*** Testing divide-by-zero during SELECT';  
GO  
-- Returns NULL.
SELECT a / b AS ab    
FROM t1;  
GO  

PRINT '*** Testing divide-by-zero during INSERT';  
GO  
-- Fails with NOT NULL violation.
INSERT INTO t2  
SELECT a / b AS ab    
FROM t1;  
GO  
PRINT '*** Testing tinyint overflow';  
GO  
-- Fails with NOT NULL violation
INSERT INTO t2  
SELECT a + b AS ab   
FROM t1;  
GO  

PRINT '*** Resulting data - should be 0 rows';  
GO  
SELECT *   
FROM t2;  
GO  

-- Drop tables t1 and t2 and restore ANSI_WARNINGS
DROP TABLE t1;  
DROP TABLE t2;  
SET ANSI_WARNINGS ON
GO