Questions tagged [context-info]

Returns the context_info value that was set for the current session or batch by using the SET CONTEXT_INFO statement.

Syntax

 CONTEXT_INFO()

Return Value

The value of context_info, or NULL if context_info was not set.

Remarks

Multiple active result sets (MARS) enables applications to run multiple batches, or requests, at the same time on the same connection. When one of the batches on a MARS connection runs SET CONTEXT_INFO, the new context value is returned by the CONTEXT_INFO function when it is run in the same batch as the SET statement. The new value is not returned by the CONTEXT_INFO function run in one or more of the other batches on the connection, unless they started after the batch that ran the SET statement completed.


Permissions

Requires no special permissions. The context information is also stored in the sys.dm_exec_requests, sys.dm_exec_sessions, and sys.sysprocesses system views, but querying the views directly requires SELECT and VIEW SERVER STATE permissions.


Examples

The following simple example sets the context_info value to 0x1256698456, and then uses the CONTEXT_INFO function to retrieve the value.

DECLARE @UserID BIGINT
SET CONTEXT_INFO @UserID
SELECT CAST(CAST(CONTEXT_INFO() AS BINARY(8)) AS INT)
16 questions
26
votes
2 answers

What is the scope of CONTEXT_INFO in SQL Server?

I am using CONTEXT_INFO to pass a username to a delete trigger for the purposes of an audit/history table. I'm trying to understand the scope of CONTEXT_INFO and if I am creating a potential race condition. Each of my database tables has a stored…
JasonS
  • 23,480
  • 9
  • 41
  • 46
14
votes
4 answers

Casting CONTEXT_INFO to varchar and the resulting length

I'm trying to use CONTEXT_INFO to pass a usercode from a stored procedure into a DELETE trigger for table auditing purposes. It all works fine, however I noticed that the length of the usercode saved in the audit table was not correct. Take this…
Brett Postin
  • 11,215
  • 10
  • 60
  • 95
3
votes
1 answer

Replacement of Context_Info SQL Azure

I am currently using the CONTEXT_INFO property of the Master database for storing the logged in username to use it later in Table Triggers for auditing. While migrating to SQL Azure, the issue of Cross-Database connections popped and I couldn't find…
2
votes
2 answers
2
votes
0 answers

How to track CONTEXT_INFO in SQL Server Profiler?

I am working with SQL Server 2008 R2. If I set some context info for the current session, how can I track it in SQL Server Profiler? Which column in SQL Server Profiler will show me the value of the context info? For example suppose in a stored…
srh
  • 1,661
  • 4
  • 30
  • 57
1
vote
1 answer

Weird result coverting from varchar to varbinary and converting back -- sql server 2008

Here is the stored procedure: CREATE PROCEDURE dbo.TestTestTest AS BEGIN DECLARE @ProcedureIdForTracking varbinary(128) = CONVERT(varbinary(128), @@procid) DECLARE @ProcedureNameForTracking varbinary(128) =…
wxw
  • 45
  • 5
1
vote
0 answers

Context_info value is null in trigger

I want to get some value in trigger when its start executing. However getting the value using parameter option is not available in trigger. So I decided to store that particular value in one variable and pass it to Context_info in my stored…
1
vote
1 answer

Why context_info can fail?

I am using context_info to simulate a global variable, I use it in this context. I have scripts made up of several batches, I set a global variable at the beginning (123 in the example) and then after all the batches I use that number to populate a…
UnDiUdin
  • 14,924
  • 39
  • 151
  • 249
1
vote
1 answer

How to store and extract number from binary field?

I have a procedure that is doing INSERT/DELETE operations over table and a trigger which is logging the changes in second table. What I need is to pass the security user id that instantiated the procedure call to the trigger in order to record who…
gotqn
  • 42,737
  • 46
  • 157
  • 243
0
votes
0 answers

How to set countext_info with linked server connection

I am connecting to a server with linked server (I call this server customerServer) , I must set context info before exec a sp in CustomerServer...but I can't set context info by linkserver....what shoud I do? this is the Context_infi and the…
0
votes
1 answer

How to cast context_info return value to xml

How can I do cast/convert context_info return value data to XML? The first query works fine and query2 does not work -- Query 1 DECLARE @xml XML = '' DECLARE @varB VARBINARY(128); SET @varB = CAST(@xml AS…
AAA
  • 21
  • 6
0
votes
1 answer

bad string in dynamic sql and context info

This is so strange! The following code is a t-sql. BEGIN DECLARE @cinfo VARBINARY(128) = CAST('aud:83/53784862/1' AS VARBINARY(128)); DECLARE @csinfo VARCHAR(128) = CAST(CONTEXT_INFO() AS VARCHAR(128)); SET @csinfo = '' +…
vipcxj
  • 840
  • 5
  • 10
0
votes
0 answers

How to get value inside trigger

i'm running my application using one DB but during some specific table insertion i'm calling trigger where i need to get some value from some another DB.Previously i did this by hard coding the specific DB name inside the trigger. i got it correctly…
0
votes
2 answers

SQL Server, CONTEXT_INFO(), and varchar size

In table users I have a column username of datatype varchar(50). The table has no records. I insert a new record with A for the username. The following returns what I would expect: SELECT username, LEN(username) FROM users WHERE id = 1 -- returns:…
Developer Webs
  • 983
  • 9
  • 29
0
votes
0 answers

USING CONTEX_INFO SQL SERVER

DECLARE @Ctx varbinary(128) DECLARE @username varchar(30) SET @Username='ibica' SELECT @Ctx = CONVERT(varbinary(128), @Username) SET CONTEXT_INFO @Ctx SET @username = CONVERT(VarChar(128), CONTEXT_INFO()); PRINT @username DECLARE…
Tanatos Daniel
  • 558
  • 2
  • 9
  • 27
1
2