Starting from SQL Server 2016
a new way for sharing information in session is introduced via the SESSION_CONTEXT and sp_set_session_context.
You can use them as alternative of CONTEXT_INFO()
which persist only a binary value limited to 128 bytes. Also, the user can rewrite the value anytime and it's not very good to use it for security checks.
The following issues are resolved using the new utils. You can store the data in more user-friendly format:
EXEC sp_set_session_context 'language', 'English';
SELECT SESSION_CONTEXT(N'language');
Also, we can mark it as read-only
:
EXEC sp_set_session_context 'user_id', 4, @read_only = 1;
If you try to modify a read-only
session context you will get something like this:
Msg 15664, Level 16, State 1, Procedure sp_set_session_context, Line
10 Cannot set key 'user_id' in the session context. The key has been set as
read_only for this session.