遵循我的存储过程
ALTER PROCEDURE [dbo].[Proc_userlogin] @userid VARCHAR(50),
@password VARCHAR(50)
AS
DECLARE @ReturnVal VARCHAR(500)
SET nocount ON
IF EXISTS(SELECT username,
password
FROM clients
WHERE username = @userid
AND password = Hashbytes('SHA1', @password))
SET @ReturnVal='0|Logged in Successfully'
ELSE IF NOT EXISTS(SELECT username
FROM clients
WHERE username = @userid)
SET @ReturnVal='1|Incorrect Username'
ELSE IF NOT EXISTS(SELECT username,
password
FROM clients
WHERE username = @userid
AND password = Hashbytes('SHA1', @password))
SET @ReturnVal='1|Incorrect Password'
SELECT @ReturnVal
在上面的存储过程中,我正在检查用户名和密码是否输入正确。我遇到的问题是检查多个条件。可以删除多个选择查询,并使用If条件中的单选查询来检查身份验证?。可以自定义上述存储过程,以最小化if条件下的select语句数。
请建议
由于
答案 0 :(得分:0)
我优化你的SQL代码:
ALTER PROCEDURE [dbo].[Proc_userlogin] @userid VARCHAR(50),
@password VARCHAR(50)
AS
SET nocount ON
DECLARE @ReturnVal VARCHAR(500)
DECLARE @Password VARCHAR(50)
SET @Password = NULL
SELECT @Password = password
FROM clients
WHERE username = @userid
IF ( @Password IS NULL )
SET @ReturnVal='1|Incorrect Username'
ELSE IF ( @Password = Hashbytes('SHA1', @password) )
SET @ReturnVal='0|Logged in Successfully'
ELSE
SET @ReturnVal='1|Incorrect Password'
SELECT @ReturnVal
答案 1 :(得分:0)
感谢@Mohammad您的解决方案有效但经过一些修改
ALTER procedure [dbo].[Proc_CheckUser]
@userid VARCHAR(50),
@password VARCHAR(50)
AS
SET nocount ON
DECLARE @ReturnVal VARCHAR(500)
DECLARE @PasswordOld VARCHAR(50)
SELECT @PasswordOld = password
FROM Clients
WHERE username = @userid
IF ( @PasswordOld IS NULL )
SET @ReturnVal='1|Incorrect Username'
ELSE IF ( @PasswordOld = Hashbytes('SHA1', @password) )
SET @ReturnVal='0|Logged in Successfully'
ELSE
SET @ReturnVal='2|Incorrect Password'
SELECT @ReturnVal