Home > Software engineering >  Set session attribute that is readable from external
Set session attribute that is readable from external

Time:10-14

In Oracle it is possible from PLSQL to set an attribute (CLIENT_INFO) in the session. This allows it to make the link between the session object and the running package/stored procedure. The package to do this is the DBMS_APPLICATION_INFO package

So executing in the client session:

exec DBMS_APPLICATION_INFO.SET_CLIENT_INFO('hello stackoverflow');

allows it to see in an admin session view :

select sid, logon_time, client_info 
from v$session 
where client_info like '%stack%' ;

       SID LOGON_TIME          CLIENT_INFO
---------- ------------------- ----------------------------------------------------------------
      2549 2022-10-13 20:39:42 hello stackoverflow

Is there something equivalent for SQL Server that can be used via T-SQL?

CodePudding user response:

The equivalent in SQL Server would be:

declare @ctx varbinary(128) = cast( 'hello stackoverflow' as varbinary(128))
SET CONTEXT_INFO  @ctx 
go
select s.session_id, connect_time, cast(context_info as varchar(128)) context_info
from sys.dm_exec_sessions s
join sys.dm_exec_connections c
  on s.session_id = c.session_id 
where cast(context_info as varchar(128)) like '%stack%'
  • Related