CREATE PROCEDURE [dbo].[Sp_UserAuthenticate] @UserName SMALLINT , @Password VARCHAR(32) 
AS
SELECT Users.*, T.ConnectionId, T.SystemName, T.ProgramType FROM Users 
LEFT OUTER JOIN 
(
  SELECT TOP 1 dbo.ConnectedUser.UserId,  ConnectedUser.ConnectionId, SystemName, ProgramType FROM dbo.ConnectedUser INNER JOIN 
  dbo.vw_SqlProcesses ON vw_SqlProcesses.ConnectionId = ConnectedUser.ConnectionId  
) AS T ON T.UserId = dbo.Users.Id
WHERE UserName = @UserName AND ISNULL(Password,'') = 
CASE
  WHEN @UserName = 100 THEN ''
  ELSE @Password
END
AND IsActive = 1