@@OPTIONS(Transact-SQL)

적용 대상:SQL ServerAzure SQL DatabaseAzure SQL Managed Instance

현재 SET 옵션에 대한 정보를 반환합니다.

Transact-SQL 구문 표기 규칙

Syntax

@@OPTIONS

참고 항목

SQL Server 2014(12.x) 및 이전 버전에 대한 Transact-SQL 구문을 보려면 이전 버전 설명서를 참조 하세요.

반환 형식

integer

설명

옵션은 SET 명령의 사용 또는 sp_configure user options 값에서 비롯될 수 있습니다. SET 명령으로 구성된 세션 값이 sp_configure 옵션을 재정의합니다. Management Studio와 같은 많은 도구가 집합 옵션을 자동으로 구성합니다. 각 사용자는 구성을 나타내는 @@OPTIONS 함수를 가집니다.

SET 문을 사용하여 특정 사용자 세션에 대한 언어와 쿼리 처리 옵션을 변경할 수 있습니다. @@OPTIONS는 ON 또는 OFF로 설정된 옵션만 검색할 수 있습니다.

@@OPTIONS 함수는 10진수 정수로 변환된 옵션의 비트맵을 반환합니다. 비트 설정은 사용자 옵션 서버 구성 옵션 구성 문서의 표에 나와 있는 위치에 저장됩니다.

@@OPTIONS 값을 해독하려면 @@OPTIONS에서 반환한 정수를 이진으로 변환한 다음, 사용자 옵션 서버 구성 옵션 구성의 표에서 값을 검색합니다. 예를 들어 SELECT @@OPTIONS;에서 5496 값을 반환하는 경우 Windows 프로그래머 계산기(calc.exe)를 사용하여 10진수 5496을 이진으로 변환하세요. 결과는 1010101111000입니다. 가장 오른쪽의 문자(이진 1, 2, 4)는 0으로, 표의 맨 앞 세 항목이 해제된 상태를 나타냅니다. 표에서 해당 항목은 DISABLE_DEF_CNST_CHK, IMPLICIT_TRANSACTIONS, CURSOR_CLOSE_ON_COMMIT입니다. 다음 항목(ANSI_WARNINGS 위치의 1000)이 설정되어 있습니다. 비트맵에서 계속 왼쪽으로 작동하다가 옵션 목록의 아래로 이동합니다. 가장 왼쪽 옵션이 0이면 유형 변환에 의해 잘립니다. 비트맵 1010101111000은 실제로 001010101111000으로 모두 15개의 옵션을 나타냅니다.

예제 C@@OPTIONS 비트 마스크를 사용자 옵션에 자동으로 매핑하는 쿼리를 제공합니다.

A. 변경이 동작에 미치는 영향 설명

다음 예는 두 가지 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. 클라이언트 NOCOUNT 설정 테스트

다음 예에서는 NOCOUNT``ON을 설정한 후 @@OPTIONS의 값을 테스트합니다. NOCOUNT``ON 옵션은 세션 내의 모든 문 실행 시 영향 받은 행의 수를 표시하는 메시지를 요청 클라이언트로 다시 보내지 않도록 합니다. @@OPTIONS의 값은 512(0x0200)로 설정되며 이는 NOCOUNT 옵션을 나타냅니다. 이 예에서는 클라이언트에서 NOCOUNT 옵션을 사용할 수 있는지 테스트합니다. 이러한 방법은 예를 들어 클라이언트에서 성능 차이를 추적하는 데 도움이 될 수 있습니다.

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

C. PIVOT 쿼리를 사용하여 @@OPTIONS 비트 마스크 검토

다음 예제에서는 테이블 반환 생성자를 사용하여 숫자 목록 참조를 생성한 다음 비트 연산자와 @@OPTIONS 값을 비교합니다. APPLY 절은 문자열 연결을 수행하여 문자 비트 마스크를 생성하고, 다른 절은 사용자 옵션 서버 구성 옵션 구성에서 문서화된 값에 대해 검토할 별칭을 생성합니다.

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. GET_BIT 사용하여 @@OPTIONS 비트 마스크 검토

적용 대상: SQL Server 2022(16.x) 이상 버전

다음 예제에서는 GET_BIT 함수를 사용하여 각 특정 비트에서 값을 가져옵니다@@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

참고 항목