0

我有一个存储过程,它调用其他存储过程,然后返回一条消息。但是,SP 没有按预期工作:

ALTER PROCEDURE [dbo].[sp_Test] 
    -- Add the parameters for the stored procedure here
    @ID int,
    @UN nvarchar(30),
    @PW nvarchar(30),
    @Message nvarchar(50) out
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;
    -- Variables
    Declare @ValidUser nvarchar(75);
    Declare @Current nvarchar(50);
    Declare @Log bit = 0;
    Declare @LogDateTime datetime2 = getdate();

    Execute @ValidUser = sp_UserValidation @ID, @UN, @PW, @ValidUser;
    --sp_UserValidation returns 'Success'

    If @ValidUser = 'Success'
        Begin
            -- 2) Validate Account
            Execute @Current = sp_ValidateCurrent @ID, @Current;
            --sp_ValidateCurrent returns 'Account is current'

            If @ACurrent = 'Account is current'
                Begin
                    Set @Message = 'Success';
                End
            Else
                Begin
                    If @Current = 'Grace'
                        Begin
                            Set @Message = 'Grace';
                        End
                    Else
                        Begin
                            If @AccountCurrent = 'Not Billing'
                                Begin
                                    Set @Message = 'Success';
                                End
                            Else Set @Message = 'Failure'
                        End
                End
        End
    Else Set @Message = 'Not valid.';

    Select @Message;
END

根据我的逻辑,@Message 应该等于“成功”,但返回的是“无效”。我究竟做错了什么?请告诉我。谢谢你。

这是 sp_UserValidation 的逻辑:

ALTER PROCEDURE [dbo].[sp_UserValidation] 
-- Add the parameters for the stored procedure here
@ID int,
@UN nvarchar(30),
@PW nvarchar(30),
@Output nvarchar(50) out
AS
BEGIN

SET NOCOUNT ON;
Declare @UserCount int = 0;
Declare @AccountLocked bit = 1;
Declare @FailedAttempts int = 4;
Declare @MaxAttempts int = 3;
Declare @LastLogin datetime2;

Set @Output = 'Error: Please try again';

-- 1) Check that Account Code/UserName/Password combination exists
Select @UserCount = count(*) from tblUSER where ID = @AID AND UserName = @UN AND Password = @PW;

If @UserCount = 1
Begin
    -- 2) Check that the User Account is not locked
    -- 2.a) Get maximum allowed attempts
    Select @MaxAttempts = Value from tbl_sysTABLE where Title = 'MaxUserAttempts';

    -- 2.b) Get total failed attempts since last successful login
            -- Get Last Successful login date
    Select @LastLogin = LoginDatetime from tblLOG where ID = @ID AND UserName = @UN AND LoginDatetime in (select max(LoginDatetime) from tblLOG where ID = @ID AND UserName = @UN AND Message = 'Success');

            -- Get failed attempts count
    Select @FailedAttempts = count(*) from tblLOG where LoginDatetime > @LastLogin;

    -- 2.c) If failed attempts > maximum allowed attempts, account is locked, else account is not locked.
    If @FailedAttempts < @MaxAttempts
    Begin
        Set @Output = 'Success';
    End
    Else Set @Output = 'User account is locked.';
End

Select @Output;
END
4

1 回答 1

3

我猜你想要:

Execute sp_UserValidation @ID, @UN, @PW, @ValidUser output;

存储过程不返回字符串。

是返回值是整数的参考。我很惊讶它在编写时不会产生语法错误。我想这是因为return它也用于可以返回任何类型的函数中。

我一直使用返回作为状态指示器。在您的情况下,您正在传递一个变量然后使用它,因此它是一个输出参数是有道理的。

于 2013-06-22T14:30:45.077 回答