Reputation: 3214
I have written the following stored procedure:
ALTER PROCEDURE [dbo].[sp_Accounts_ValidateLogin]
@EmailAddress varchar(255),
@Password varchar(20)
AS
DECLARE @UserID int
SELECT @UserID = UserID
FROM Accounts_Users
WHERE EmailAddress = @EmailAddress and Password = @Password
IF @UserID != NULL
RETURN @UserID
ELSE
RETURN -1
When this procedure is executed it shows,
No rows affected.
(0 row(s) returned)
@RETURN_VALUE = -1
But whenever I rewrite the SELECT
statement as,
SELECT UserID
FROM Accounts_Users
WHERE EmailAddress = @EmailAddress and Password = @Password
The result is:
UserID
---------------------------------------------------------------------------------------
3
No rows affected.
(1 row(s) returned)
@RETURN_VALUE = -1
My question is why the variable @UserID
is not getting set inside the 1st select statement?
Upvotes: 1
Views: 281
Reputation: 25056
!= NULL will not work as you expect.
Make it:
IF @UserID IS NOT NULL
The below SO question has a little information as to why, so I won't bother reposting it (it's basically because NULL is not an actual value):
Not equal <> != operator on NULL
Upvotes: 5