Converting from nvarchar(x) to varbinary(x) back to nvarchar(x) will always be rounded. The main problem described here arises because SELECT CONTEXT_INFO() always returns 128 bytes, regardless of the length of the varbinary source used to set CONTEXT_INFO .
There are two workarounds.
First solution : starting with SQL Server 2016, sp_set_session_context can be used to set significantly superior session_context .
Second workaround : CONTEXT_INFO can be retrieved from sys.dm_exec_sessions using the sys.dm_exec_sessions CONTEXT_INFO() method. This query will return the CONTEXT_INFO associated with the current connection:
SELECT context_info FROM sys.dm_exec_sessions WHERE session_id = @@SPID
Here is a comparison of two search methods in SSMS:
SET CONTEXT_INFO 0x010200340056; GO SELECT CONTEXT_INFO();
Provision : CONTEXT_INFO setting should be performed in a separate batch, starting from receiving sys.dm_exec_sessions via sys.dm_exec_sessions (as shown above through the SSMS GO packet separator).
source share