Share via


@@OPTIONS (Transact-SQL)

Si applica a:SQL Server database SQL di Azure Istanza gestita di SQL di Azure

Restituisce informazioni sulle opzioni SET correnti.

Convenzioni di sintassi Transact-SQL

Sintassi

@@OPTIONS

Nota

Per visualizzare la sintassi Transact-SQL per SQL Server 2014 (12.x) e versioni precedenti, vedere la documentazione delle versioni precedenti.

Tipo restituito

integer

Osservazioni:

Le opzioni possono provenire dall'uso del SET comando o dal sp_configure user options valore . I valori di sessione configurati con il SET comando sostituiscono le sp_configure opzioni. Molti strumenti, ad esempio Management Studio, configurano automaticamente le opzioni impostate. Ogni utente ha una @@OPTIONS funzione che rappresenta la configurazione.

È possibile modificare il linguaggio e le opzioni di elaborazione query per una sessione utente specifica usando l'istruzione SET . @@OPTIONS può rilevare solo le opzioni impostate su ON o OFF.

La @@OPTIONS funzione restituisce una bitmap delle opzioni, convertita in un intero di base 10 (decimale). Le impostazioni di bit vengono archiviate nei percorsi descritti in una tabella nell'articolo Configurare l'opzione di configurazione del server delle opzioni utente.

Per decodificare il @@OPTIONS valore, convertire l'intero restituito da @@OPTIONS in binario e quindi cercare i valori nella tabella in Configurare l'opzione di configurazione del server delle opzioni utente. Se, ad esempio, SELECT @@OPTIONS; restituisce il valore 5496, usare la calcolatrice di Windows (calc.exe) per convertire il valore decimale 5496 in binario. Il risultato è 1010101111000. I caratteri più a destra (binario 1, 2 e 4) sono 0, a indicare che i primi tre elementi della tabella sono disattivati. Consultando la tabella, si noterà che si tratta di DISABLE_DEF_CNST_CHK, IMPLICIT_TRANSACTIONSe CURSOR_CLOSE_ON_COMMIT. L'elemento 1000 successivo (ANSI_WARNINGS nella posizione) è attivo. Continuare a usare la mappa dei bit e verso il basso nell'elenco delle opzioni. Quando le opzioni più a sinistra corrispondono a 0, vengono troncate dalla conversione dei tipi. La mappa di bit 1010101111000 è in realtà 001010101111000 per rappresentare tutte e 15 le opzioni.

L'esempio C fornisce una query che esegue automaticamente il mapping della @@OPTIONS maschera di bit alle opzioni utente.

Esempi

R. Dimostrazione di come le modifiche influiscono sul comportamento

Nell'esempio seguente viene illustrata la differenza nel comportamento di concatenazione con due diverse impostazioni dell'opzione CONCAT_NULL_YIELDS_NULL .

SELECT @@OPTIONS AS OriginalOptionsValue;
SET CONCAT_NULL_YIELDS_NULL OFF;
SELECT 'abc' + NULL AS ResultWhen_OFF, @@OPTIONS AS OptionsValueWhen_OFF;
  
SET CONCAT_NULL_YIELDS_NULL ON;
SELECT 'abc' + NULL AS ResultWhen_ON, @@OPTIONS AS OptionsValueWhen_ON;

B. Testare un'impostazione NOCOUNT del client

Nell'esempio seguente viene impostata l'opzione NOCOUNT``ON e viene quindi testato il valore di @@OPTIONS. L'opzione NOCOUNT``ON impedisce che, per ogni istruzione di una sessione, il messaggio relativo al numero di righe interessate venga inviato al client che esegue la richiesta. Il valore di @@OPTIONS viene impostato su 512 (0x0200), che rappresenta l'opzione NOCOUNT. In questo esempio viene verificato se l'opzione NOCOUNT è stata abilitata nel client. Ciò può risultare utile, ad esempio, per tenere traccia delle differenze di esecuzione in un client.

SET NOCOUNT ON
IF @@OPTIONS & 512 > 0
RAISERROR ('Current user has SET NOCOUNT turned on.', 1, 1)

C. Esaminare @@OPTIONS maschera di bit con una query PIVOT

Nell'esempio seguente vengono utilizzati costruttori con valori di tabella per generare un riferimento all'elenco di numeri e quindi confrontare il valore di @@OPTIONS con un operatore bit per bit. Una clausola APPLY esegue la concatenazione di stringhe per generare una maschera di bit di caratteri e un altro genera alias da esaminare rispetto ai valori documentati in Configurare l'opzione di configurazione del server delle opzioni utente.

SELECT S.Bits,
    Flags.*
FROM (
    SELECT optRef,
        posRef,
        flagCheck
    FROM (
        SELECT ones.n + tens.n * 10
        FROM ( VALUES (0), (1), (2), (3), (4), (5), (6), (7), (8), (9) ) ones(n),
            ( VALUES (0), (1), (2), (3), (4), (5), (6), (7), (8), (9) ) tens(n)
        ) f1(powRef)
    CROSS APPLY (
        SELECT POWER(2, powRef)
        WHERE powRef <= 16
        ) f2(binRef)
    CROSS JOIN (
        VALUES (@@OPTIONS)
        ) f3(optRef)
    CROSS APPLY (
        SELECT (optRef & binRef) / binRef
        ) f4(flagRef)
    CROSS APPLY (
        SELECT RIGHT(CONVERT(VARCHAR(2), CAST(powRef AS VARBINARY(1)), 2), 1) [posRef],
            CAST(flagRef AS INT) [flagCheck]
        ) pref
    ) TP
PIVOT( MAX( flagCheck ) FOR posRef IN ( [0], [1], [2], [3], [4], [5], [6], [7], [8], [9], [A], [B], [C], [D], [E], [F] )) P
CROSS APPLY (
    SELECT CONCAT ( '', [0], [1], [2], [3], [4], [5], [6], [7], [8], [9], [A], [B], [C], [D], [E], [F] ),
        CONCAT ( '', [F], [E], [D], [C], [B], [A], [9], [8], [7], [6], [5], [4], [3], [2], [1], [0] )
    ) S (stib, Bits)
CROSS APPLY (
    SELECT
          CAST(P.[0] AS BIT) /* 1     */ [DISABLE_DEF_CNST_CHK] -- Controls interim or deferred constraint checking.
        , CAST(P.[1] AS BIT) /* 2     */ [IMPLICIT_TRANSACTIONS] -- For dblib network library connections, controls whether a transaction is started implicitly when a statement is executed. The IMPLICIT_TRANSACTIONS setting has no effect on ODBC or OLEDB connections.
        , CAST(P.[2] AS BIT) /* 4     */ [CURSOR_CLOSE_ON_COMMIT] -- Controls behavior of cursors after a commit operation has been performed.
        , CAST(P.[3] AS BIT) /* 8     */ [ANSI_WARNINGS] -- Controls truncation and NULL in aggregate warnings.
        , CAST(P.[4] AS BIT) /* 16    */ [ANSI_PADDING] -- Controls padding of fixed-length variables.
        , CAST(P.[5] AS BIT) /* 32    */ [ANSI_NULLS] -- Controls NULL handling when using equality operators.
        , CAST(P.[6] AS BIT) /* 64    */ [ARITHABORT] -- Terminates a query when an overflow or divide-by-zero error occurs during query execution.
        , CAST(P.[7] AS BIT) /* 128   */ [ARITHIGNORE] -- Returns NULL when an overflow or divide-by-zero error occurs during a query.
        , CAST(P.[8] AS BIT) /* 256   */ [QUOTED_IDENTIFIER] -- Differentiates between single and double quotation marks when evaluating an expression.
        , CAST(P.[9] AS BIT) /* 512   */ [NOCOUNT] -- Turns off the message returned at the end of each statement that states how many rows were affected.
        , CAST(P.[A] AS BIT) /* 1024  */ [ANSI_NULL_DFLT_ON] -- Alters the session's behavior to use ANSI compatibility for nullability. New columns defined without explicit nullability are defined to allow nulls.
        , CAST(P.[B] AS BIT) /* 2048  */ [ANSI_NULL_DFLT_OFF] -- Alters the session's behavior not to use ANSI compatibility for nullability. New columns defined without explicit nullability do not allow nulls.
        , CAST(P.[C] AS BIT) /* 4096  */ [CONCAT_NULL_YIELDS_NULL] -- Returns NULL when concatenating a NULL value with a string.
        , CAST(P.[D] AS BIT) /* 8192  */ [NUMERIC_ROUNDABORT] -- Generates an error when a loss of precision occurs in an expression.
        , CAST(P.[E] AS BIT) /* 16384 */ [XACT_ABORT] -- Rolls back a transaction if a Transact-SQL statement raises a run-time error.*/
    ) AS Flags;

D. Esaminare @@OPTIONS maschera di bit con GET_BIT

Si applica a: SQL Server 2022 (16.x) e versioni successive.

Nell'esempio seguente viene usata la funzione GET_BIT per ottenere il valore da ogni bit specifico in @@OPTIONS.

SELECT
      GET_BIT(@@OPTIONS, 0)  /* 1     */ AS [DISABLE_DEF_CNST_CHK] -- Controls interim or deferred constraint checking.
    , GET_BIT(@@OPTIONS, 1)  /* 2     */ AS [IMPLICIT_TRANSACTIONS] -- For dblib network library connections, controls whether a transaction is started implicitly when a statement is executed. The IMPLICIT_TRANSACTIONS setting has no effect on ODBC or OLEDB connections.
    , GET_BIT(@@OPTIONS, 2)  /* 4     */ AS [CURSOR_CLOSE_ON_COMMIT] -- Controls behavior of cursors after a commit operation has been performed.
    , GET_BIT(@@OPTIONS, 3)  /* 8     */ AS [ANSI_WARNINGS] -- Controls truncation and NULL in aggregate warnings.
    , GET_BIT(@@OPTIONS, 4)  /* 16    */ AS [ANSI_PADDING] -- Controls padding of fixed-length variables.
    , GET_BIT(@@OPTIONS, 5)  /* 32    */ AS [ANSI_NULLS] -- Controls NULL handling when using equality operators.
    , GET_BIT(@@OPTIONS, 6)  /* 64    */ AS [ARITHABORT] -- Terminates a query when an overflow or divide-by-zero error occurs during query execution.
    , GET_BIT(@@OPTIONS, 7)  /* 128   */ AS [ARITHIGNORE] -- Returns NULL when an overflow or divide-by-zero error occurs during a query.
    , GET_BIT(@@OPTIONS, 8)  /* 256   */ AS [QUOTED_IDENTIFIER] -- Differentiates between single and double quotation marks when evaluating an expression.
    , GET_BIT(@@OPTIONS, 9)  /* 512   */ AS [NOCOUNT] -- Turns off the message returned at the end of each statement that states how many rows were affected.
    , GET_BIT(@@OPTIONS, 10) /* 1024  */ AS [ANSI_NULL_DFLT_ON] -- Alters the session's behavior to use ANSI compatibility for nullability. New columns defined without explicit nullability are defined to allow nulls.
    , GET_BIT(@@OPTIONS, 11) /* 2048  */ AS [ANSI_NULL_DFLT_OFF] -- Alters the session's behavior not to use ANSI compatibility for nullability. New columns defined without explicit nullability do not allow nulls.
    , GET_BIT(@@OPTIONS, 12) /* 4096  */ AS [CONCAT_NULL_YIELDS_NULL] -- Returns NULL when concatenating a NULL value with a string.
    , GET_BIT(@@OPTIONS, 13) /* 8192  */ AS [NUMERIC_ROUNDABORT] -- Generates an error when a loss of precision occurs in an expression.
    , GET_BIT(@@OPTIONS, 14) /* 16384 */ AS [XACT_ABORT] -- Rolls back a transaction if a Transact-SQL statement raises a run-time error.*/
GO

Vedi anche