2013-06-22 153 views
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 
+2

你可以发布'sp_UserValidation'的代码吗?或者它总是返回'成功'?当'@ Message'是'无效'时,显然唯一的一次是'@ ValidUser'不等于'Success'。 –

+1

[Creating Stored Procedures](http://msdn.microsoft.com/zh-cn/library/ms190669(v = sql.105).aspx):“我们建议您不要使用** sp_创建任何存储过程**作为前缀SQL Server使用** sp _ **前缀来指定系统存储过程,您选择的名称可能与某些将来的系统过程冲突。 –

+0

sp_UserValidation代码已发布到主文章。另外,感谢您提供sp_前缀建议,我将更改该命名约定。 – user2511772

回答

3

我猜测你想要的:

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

存储过程不返回字符串。

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

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

+0

相关信息+1 –

+1

将整数隐式转换为'varchar',因此它最终会以变量中的字符串“0”结尾。 OP实际上不能做'RETURN'成功',因为这会导致无效的转换错误。 –

+0

@MartinSmith。 。我实际上在SQL小提琴上尝试了'返回'成功',它工作。也许这是该环境的一个特点,但它让我非常惊讶。 –