DF017: CONTEXT_INFO is used.
Last modified: May 28, 2025
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');
Want to find out more?
Overview
Take a quick tour to learn all about the key benefits delivered by dbForge Studio for SQL Server.
All features
Get acquainted with the rich features and capabilities of the tool in less than 5 minutes.
Request a demo
If you consider employing this tool for your business, request a demo to see it in action.