我有一个存储过程调用其他存储过程,然后返回一条消息。但是,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
根据我的逻辑,@ Messess应该等于'Success',但是返回'Not valid'。我究竟做错了什么?请告诉我。谢谢。
以下是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
答案 0 :(得分:3)
我猜你想要:
Execute sp_UserValidation @ID, @UN, @PW, @ValidUser output;
存储过程不返回字符串。
Here是返回值为整数的引用。我很惊讶它写它时不会产生语法错误。我想这是因为return
也用在可以返回任何类型的函数中。
我一直使用return作为状态指示器。在你的情况下,你传入一个变量然后使用它,所以它是一个输出参数是有道理的。