CREATE FUNCTION [dbo].[SessionUser](@spid SMALLINT = NULL , @ProgramType BIT = NULL)
RETURNS SMALLINT
AS
BEGIN
 
 RETURN ( SELECT TOP 1 UserName
		FROM    vw_SqlProcesses proccss
        INNER JOIN ConnectedUser ON ConnectedUser.ConnectionId = proccss.ConnectionId
        INNER JOIN dbo.Users ON Users.Id = ConnectedUser.UserId
		WHERE proccss.SessionId = @spid AND ProgramType = @ProgramType  )

END