DF017: CONTEXT_INFO is used.

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

Overview

Take a quick tour to learn all about the key benefits delivered by dbForge Studio for SQL Server.
All Features

All features

Get acquainted with the rich features and capabilities of the Studio in less than 5 minutes.
Request a demo

Request a demo

If you consider employing the Studio for your business, request a demo to see it in action.
Ready to start using dbForge Studio for SQL Server?