我编写了以下存储过程:
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
执行此程序时,显示
No rows affected.
(0 row(s) returned)
@RETURN_VALUE = -1
但每当我将SELECT
语句重写为
SELECT UserID
FROM Accounts_Users
WHERE EmailAddress = @EmailAddress and Password = @Password
结果是:
UserID
---------------------------------------------------------------------------------------
3
No rows affected.
(1 row(s) returned)
@RETURN_VALUE = -1
我的问题是第一次选择语句中变量@UserID
未设置的原因?
答案 0 :(得分:5)
!= NULL将无法正常工作。
成功:
IF @UserID IS NOT NULL
下面的SO问题有一些关于原因的信息,所以我不打算重新发布它(这基本上是因为NULL不是实际值):