EXEMPLO_1212310
Database
Tables
(current)
Columns
Constraints
Relationships
Orphan Tables
Anomalies
Routines
fn_GetContext
Parameters
Name
Type
Mode
Definition
CREATE FUNCTION fn_GetContext() /*Version=07.08.19*/ RETURNS @contextValue TABLE ( USERNAME NVARCHAR(60) ,PROCESSNAME NVARCHAR(128) ,NETUSER NVARCHAR(128) ,HOSTNAME NVARCHAR(128) ,APPNAME NVARCHAR(128) ,IPADDRESS VARCHAR(45) ,SESSIONID VARCHAR(10) ) AS begin declare @context varbinary(128) ,@context_info varchar(128) ,@username nvarchar(128) ,@processname nvarchar(128) ,@netuser nvarchar(128) ,@hostname nvarchar(128) ,@appname nvarchar(128) ,@ipaddress varchar(45) ,@sessionId varchar(10) ,@p1 int ,@p2 int ,@p3 int ,@p4 int ,@p5 int /* ==================================================================================================================*/ /* Prepara o contexto */ /* ==================================================================================================================*/ /* Context Infor */ IF CHARINDEX(N'#1', cast(CONTEXT_INFO() as varchar(128))) > 0 AND CHARINDEX(N'#2', cast(CONTEXT_INFO() as varchar(128))) > 0 AND CHARINDEX(N'#3', cast(CONTEXT_INFO() as varchar(128))) > 0 AND CHARINDEX(N'#4', cast(CONTEXT_INFO() as varchar(128))) > 0 BEGIN /* ==================================================================================================================*/ /* Captura o conexto da sessão */ /* ==================================================================================================================*/ set @context_info = cast(CONTEXT_INFO() as varchar(128)) /* ==================================================================================================================*/ /* Seta os itens de contexto */ /* ==================================================================================================================*/ set @p1 = charindex(N'#1', @context_info) + power(0, charindex(N'#1', @context_info)) set @p2 = charindex(N'#2', @context_info) + power(0, charindex(N'#2', @context_info)) set @p3 = charindex(N'#3', @context_info) + power(0, charindex(N'#3', @context_info)) set @p4 = charindex(N'#4', @context_info) + power(0, charindex(N'#4', @context_info)) /* -= UserName =- */ set @username = substring(@context_info, 1, @p1 - 1) /* -= ProcessName =- */ set @processname = substring(@context_info, @p1 + 2, @p2 - @p1 - 2) /* -= NetworkCredential =- */ set @netuser = substring(@context_info, @P2 + 2, @P3 - @P2 - 2) /* -= Hostname =- */ set @hostname = substring(@context_info, @P3 + 2, @P4 - @P3 - 2) /* -= AppName =- */ set @appname = APP_NAME() /* -= IpAddress =- */ set @ipaddress = substring(@context_info, @P4 + 2, len(@context_info) - 2) /* -= Session Id =- */ set @sessionId = @@SPID END ELSE BEGIN set @username = cast(isnull(original_login(),'') as varchar(64)) /* ( UserName ) */ set @processname = case when charindex(program_name(), app_name()) = 1 then cast(isnull(app_name(),'') as varchar(64)) else cast(isnull(app_name(),'') + ' - ' + isnull(program_name(),'') as varchar(128)) end /* ( ProcessName ) */ set @netuser = CAST(isnull(system_user,'') AS VARCHAR(64)) /* ( NetworkUser ) */ set @hostname = cast(isnull(host_name(),'') as varchar(64)) /* ( HostName ) */ set @appname = APP_NAME() set @ipaddress = cast(isnull(CONNECTIONPROPERTY('client_net_address'),'') as varchar(128)) COLLATE SQL_Latin1_General_CP1_CI_AS /* ( IP Address ) */ set @sessionId = @@SPID END INSERT INTO @contextValue select @username , @processname , @netuser , @hostname ,@appname, @ipaddress, @sessionId; return END