我有一个存储过程调用其他存储过程,然后返回一条消息。然而,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
你可以发布'sp_UserValidation'的代码吗?或者它总是返回'成功'?当'@ Message'是'无效'时,显然唯一的一次是'@ ValidUser'不等于'Success'。 –
[Creating Stored Procedures](http://msdn.microsoft.com/zh-cn/library/ms190669(v = sql.105).aspx):“我们建议您不要使用** sp_创建任何存储过程**作为前缀SQL Server使用** sp _ **前缀来指定系统存储过程,您选择的名称可能与某些将来的系统过程冲突。 –
sp_UserValidation代码已发布到主文章。另外,感谢您提供sp_前缀建议,我将更改该命名约定。 – user2511772