2012-03-14 77 views
0

我可以用更好的方式重寫這些語句,使用一個更新語句或其中的某個案例,或者我只是將它們保留下來幷包裝在一個事務中?我可以用更好的方式重寫這些語句嗎?

DECLARE @currentFailedPassCount int = 
     (SELECT FailedPasswordAttemptCount FROM Users WHERE [email protected]); 
UPDATE Users 
     SET FailedPasswordAnswerAttemptCount=FailedPasswordAnswerAttemptCount+1 
     WHERE [email protected]; 
IF(@currentFailedPassCount=5) 
     UPDATE Users 
     SET IsLockedOut=1 
     WHERE [email protected]; 

回答

4

事情是這樣的:

UPDATE Users 
SET FailedPasswordAnswerAttemptCount=FailedPasswordAnswerAttemptCount+1, 
    IsLockedOut = CASE 
        WHEN FailedPasswordAnswerAttemptCount >= 4 THEN 1 
        ELSE 0 
        END 
WHERE [email protected]; 

我不認爲它的內容很好(個人喜好),但它是一個單獨的語句。

+0

您可以使用2008'FailedPasswordAnswerAttemptCount + = 1'。 – 2012-03-14 19:26:43

0

嘗試:

DECLARE @currentFailedPassCount int = (SELECT FailedPasswordAttemptCount FROM Users  WHERE [email protected]); 

IF(@currentFailedPassCount=4) 
BEGIN 
    UPDATE Users 
    SET FailedPasswordAnswerAttemptCount=5, IsLockedOut=1 
    WHERE [email protected]; 
END 
ELSE 
BEGIN 
    UPDATE Users 
    SET FailedPasswordAnswerAttemptCount=FailedPasswordAnswerAttemptCount+1 
    WHERE [email protected]; 
END 
0
UPDATE Users 
SET 
    FailedPasswordAnswerAttemptCount=FailedPasswordAnswerAttemptCount + 1, 
    IsLockedOut=case 
     when FailedPasswordAnswerAttemptCount = 4 then 1 
     else IsLockedOut 
    end 
WHERE [email protected] 
相關問題