我有一个用例,我必须从passwordhistory表中获取最后五个密码,并且有两个列一个md5加密,第二个是哈希,在检索五个记录之后我必须将这些列与新密码进行比较,移动后它们来自md5和hash函数,如果找到任何记录则返回true,否则返回false。
DECLARE @UserID INT = 6
DECLARE @Password NVARCHAR(200)='admin1952'
SELECT *
FROM ( SELECT TOP 5
ChangedPassword AS ChangedPassword ,
PasswordChangeHistory.Password_Hash AS Password_Hash
FROM PasswordChangeHistory WITH ( NOLOCK )
INNER JOIN [User] U WITH ( NOLOCK ) ON PasswordChangeHistory.UserID = U.Id
WHERE PasswordChangeHistory.UserID = @UserID
) AS result
WHERE PasswordChangeHistory.ChangedPassword = dbo.Encrypt_MD5(@Password)
答案 0 :(得分:1)
要回答主题行中的问题,这里有一种方法可以做到(在伪代码中):
IF EXISTS( {your query to return a match from the last 5 passwords} )
SELECT 1;
ELSE
SELECT 0;
或者你当然可以用'True'和'False'代替1和0,如果这是你真正想要的。
答案 1 :(得分:0)
您也可以尝试查询:
if @@rowcount >0
select 'user found'
else
select 'user not found'
答案 2 :(得分:0)
这是解决问题的方法。正如我之前解释的那样,我需要从表中获取五条记录,然后用现有的五条记录检查新密码,如果这个新密码与之前的密码匹配,则返回false,否则返回true。
DECLARE @UserID INT = 6
DECLARE @Password NVARCHAR(200)= 'admin1958'
SELECT * FROM
( SELECT TOP(5) ChangedPassword AS Changed ,PasswordChangeHistory.Password_Hash AS Password_Hash
FROM PasswordChangeHistory WITH ( NOLOCK )
INNER JOIN [User] U WITH ( NOLOCK ) ON PasswordChangeHistory.UserID = U.Id
WHERE PasswordChangeHistory.UserID = @UserID
ORDER BY PasswordChangeHistory.ID DESC
) AS result
WHERE result.Changed= dbo.Encrypt_MD5(@Password) or result.Password_Hash = dbo.Encrypt_SHA2(@Password)