Please help me with this:
This code works fine except for cases in which the control goes inside the IF block of "NOT EXSISTS", then any query run after the execution of this block causes the sql connection to forcibly get closed, although the results from the running of this code block are correct. I can not run other queries after running it.
IF(@Mode='Get')
BEGIN
IF(@Field='manager')
BEGIN
DECLARE @UserUserName NVARCHAR(250)
DECLARE @UserID AS VARCHAR(50)
SELECT @UserUserName=CAST(Value AS NVARCHAR(250)) FROM dbo.UserProperties WHERE [Key]=@Key AND Field='manager'
IF(NOT EXISTS(SELECT * FROM dbo.Users WHERE UserUsername=@UserUserName) OR @UserUserName IS NULL )
BEGIN
SELECT @UserID = dbo.fnGetManagerId(CAST(@Key AS INT)) -- numeric
SELECT @UserUserName=UserUsername FROM dbo.Users WHERE UserID=@UserID
END
SELECT UserName AS Value FROM users WHERE UserUsername=@UserUserName
END
ELSE
BEGIN
SELECT Value FROM dbo.UserProperties WHERE [Key]=@Key AND Field=@Field
END
END