1
I’m using the table sys.sysprocesses
in the SQL 2012/2014
, to identify through the field CONTEXT_INFO
, which user of a particular application is tied to a particular session.
I created a function by passing as parameter the SPID
.
CREATE FUNCTION [dbo].[FC_RETORNAUSUARIO] (@spid INTEGER)
RETURNS sysname
WITH EXECUTE AS CALLER
AS
BEGIN
DECLARE @username sysname;
SET @username = suser_sname() DECLARE @contextinfo varbinary(128)
SELECT @contextinfo = context_info
FROM master.dbo.sysprocesses
WHERE spid = @spid DECLARE @offset int DECLARE @usernamelength int
SET @usernamelength = substring(@contextinfo, datalength(@contextinfo), 1)
SET @offset = datalength(@contextinfo) - @usernamelength - sign(@usernamelength) IF @usernamelength <> 0x00
SET @username = convert(sysname, substring(@contextinfo, @offset, @usernamelength))
SET @offset = @offset - 1 RETURN @username;
END;
From this, I did a test with the application installed locally and together the instance used to connect and it worked. I got him to pass the application user.
But when putting on a server, I realized that the information from CONTEXT_INFO
came empty. And that the user of the database in which the application was connected had no integration with Windows, IE, was a user created only for access to the bank.
The fact that the user does not have integration with Windows (not from the server domain) is what makes the context_info
is not completed?