DF017: CONTEXT_INFO is used.
Last modified: December 25, 2024
The topic describes the DF017 T-SQL code analysis rule.
Category
BEST PRACTICE
Message
CONTEXT_INFO is used.
Description
It is not recommended to use CONTEXT_INFO because binary data may be corrupted. Use SESSION_CONTEXT instead.
Additional information
The behavior of SESSION_CONTEXT with MARS (Multiple Active Result Sets) is similar to that of CONTEXT_INFO. If a MARS batch updates a key-value pair, the new value isn’t available in other MARS batches on the same connection until they start after the updating batch is completed. When multiple MARS batches run on a connection, values cannot be set as “read_only” to avoid race conditions and uncertainty about which value takes precedence.
Noncompliant code example
DECLARE @user_id VARBINARY(128);
SET @user_id = cast(4 AS VARBINARY(128));
SET context_info @user_id;
SELECT CONTEXT_INFO() AS user_id;
Compliant solution
EXEC sp_set_session_context 'user_id', 4;
SELECT session_context(N'user_id');
Was this page helpful?