如果EXISTS然后ELSE与INSERT,SQL Server

时间:2016-12-19 20:54:58

标签: sql-server stored-procedures

我之前的question最终没有回答我的长期问题,即:

我需要将li:hover ul { display: block; position: absolute; padding:0px; } 密码(User_Passwords)与传入的新密码(up_Password)进行比较。如果它们匹配,我需要返回@CurrentPassword,以便代码跟随不同的轨道,而如果它是新密码,则它将1进入表格。这是我认为正在执行的存储过程的一部分:

INSERTed

我已经做了很多测试,我得到的结论是,这总是会返回DECLARE @UserNumberOfPasswords INT DECLARE @ua_pk uniqueidentifier DECLARE @ResetDaysAmount INT DECLARE @AllowedNumberOfPasswords INT DECLARE @CurrentPasswordDate DATE DECLARE @CurrentPassword varchar(25) SELECT * FROM dbo.User_Passwords WHERE ua_fk = @ua_pk ORDER BY up_PasswordDate DESC DELETE FROM User_Passwords WHERE @ua_pk = ua_fk AND @CurrentPasswordDate > (DATEADD(DAY, (@ResetDaysAmount - 1), up_PasswordDate)) SELECT * FROM dbo.User_Passwords WHERE ua_fk = @ua_pk ORDER BY up_PasswordDate ASC SELECT @UserNumberOfPasswords = COUNT(*) FROM User_Passwords AS up WHERE ua_fk = @ua_pk IF @UserNumberOfPasswords > @AllowedNumberOfPasswords BEGIN WITH T AS (SELECT TOP (@UserNumberOfPasswords - @AllowedNumberOfPasswords) * FROM User_Passwords WHERE ua_fk = @ua_pk ORDER BY up_PasswordDate ASC) DELETE FROM T; END IF @UserNumberOfPasswords = @AllowedNumberOfPasswords BEGIN WITH T AS (SELECT TOP 1 * FROM User_Passwords WHERE ua_fk = @ua_pk ORDER BY up_PasswordDate ASC) DELETE FROM T; END IF EXISTS (SELECT 1 FROM User_Passwords WHERE up_Password = @CurrentPassword) SELECT 1 ELSE INSERT INTO User_Passwords (ua_fk, up_Password, up_PasswordDate) VALUES (@ua_pk, @CurrentPassword, @CurrentPasswordDate ,我不能完全确定原因。

底线 - 我想比较值,如果值相等,请返回VB.Net并使用一个值,以便我可以将其用于不同的代码路由,而如果它们不相等,我将添加然后返回我的VB.Net代码。

3 个答案:

答案 0 :(得分:1)

DECLARE @result integer;   

IF EXISTS         (SELECT 1
                   FROM User_Passwords
                   WHERE up_Password = @CurrentPassword)
  BEGIN
    SET @result = 1;
  END;
ELSE
 BEGIN
    INSERT INTO User_Passwords (ua_fk, up_Password, up_PasswordDate)
    VALUES (@ua_pk, @CurrentPassword, @CurrentPasswordDate);
    SET @result = 0;
 END;

RETURN @result;

答案 1 :(得分:1)

而不是

IF EXISTS 
    (SELECT 1
    FROM User_Passwords
    WHERE up_Password = @CurrentPassword)
    SELECT 1
ELSE
    INSERT INTO User_Passwords (ua_fk, up_Password, up_PasswordDate)
    VALUES (@ua_pk, @CurrentPassword, @CurrentPasswordDate

不应该

IF EXISTS 
    (SELECT 1
    FROM User_Passwords
    WHERE up_Password = @CurrentPassword
    AND   ua_fk = @ua_pk ) //Only search for passwords that belong to this user
    SELECT 1
ELSE
    INSERT INTO User_Passwords (ua_fk, up_Password, up_PasswordDate)
    VALUES (@ua_pk, @CurrentPassword, @CurrentPasswordDate

...

否则,如果任何用户(不仅仅是感兴趣的用户)存在密码,则存储过程将返回1,这可能会解释您的结果。

答案 2 :(得分:0)

如果我需要更多调试

,我通常会添加一个打印件
IF EXISTS 
    (SELECT 1
    FROM User_Passwords
    WHERE up_Password = @CurrentPassword)
    BEGIN
        PRINT ('EXISTS')
        PRINT ('up_Password:' + up_Password)
        PRINT ('@CurrentPassword:' + @CurrentPassword)
    SELECT 1
    END
ELSE
    BEGIN
        PRINT ('NOT EXISTS')
        PRINT ('up_Password:' + up_Password)
        PRINT ('@CurrentPassword:' + @CurrentPassword)
        INSERT INTO User_Passwords (ua_fk, up_Password, up_PasswordDate)
        VALUES (@ua_pk, @CurrentPassword, @CurrentPasswordDate)
    END