我能以更好的方式重写这些陈述吗?

时间:2012-03-14 16:55:48

标签: sql-server-2008 tsql

我能否以更好的方式重写这些语句,使用一个带有案例的更新语句或者其他东西,或者我只是将它们留下来并包装在一个事务中?

 DECLARE @currentFailedPassCount int = 
       ( SELECT FailedPasswordAttemptCount FROM Users WHERE Username=@username );
 UPDATE Users 
      SET FailedPasswordAnswerAttemptCount=FailedPasswordAnswerAttemptCount+1 
      WHERE Username=@username;
 IF(@currentFailedPassCount=5) 
      UPDATE Users 
      SET IsLockedOut=1 
      WHERE Username=@username;

3 个答案:

答案 0 :(得分:4)

这样的事情:

UPDATE Users
SET FailedPasswordAnswerAttemptCount=FailedPasswordAnswerAttemptCount+1,
    IsLockedOut = CASE
                     WHEN FailedPasswordAnswerAttemptCount >= 4 THEN 1
                     ELSE 0
                  END
WHERE Username=@username;

我认为它看起来不太好(个人偏好),但它只是一个声明。

答案 1 :(得分:0)

尝试:

DECLARE @currentFailedPassCount int = ( SELECT FailedPasswordAttemptCount FROM Users      WHERE Username=@username );

IF(@currentFailedPassCount=4)
BEGIN
    UPDATE Users
    SET FailedPasswordAnswerAttemptCount=5, IsLockedOut=1
    WHERE Username=@username;
END
ELSE
BEGIN
    UPDATE Users
    SET FailedPasswordAnswerAttemptCount=FailedPasswordAnswerAttemptCount+1
    WHERE Username=@username;
END

答案 2 :(得分:0)

UPDATE Users 
SET 
    FailedPasswordAnswerAttemptCount=FailedPasswordAnswerAttemptCount + 1, 
    IsLockedOut=case 
        when FailedPasswordAnswerAttemptCount = 4 then 1 
        else IsLockedOut 
    end
WHERE Username=@username