我有一个存储过程,它调用其他存储过程,然后返回一条消息。但是,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