如何从存储过程中的存储过程调用中获取单个列的值?

时间:2016-11-07 21:07:24

标签: sql sql-server tsql stored-procedures sql-server-2012

我有以下程序:

SP_Validate_User

根据传递给SELECT someNumber AS ErrorCode, otherNumber AS AttemptsRemaining, thirdNumber AS LockoutTimeRemaining RETURN 的数据的有效性,2个语句中的1个将被执行:

SELECT * 
FROM Users 
WHERE ...

RETURN

SP_Validate_User_Main

我试图扩展我的第一个程序SP_Validate_User_Main,以便在每次用户尝试登录时将记录插入到新的审核表中,无论其是否成功。所以我已将这2行添加到INSERT INTO LoginAudit (IP, Username, Password, AttemptDate, Result) VALUES (@IP, @UserName, @Password, GETDATE(), @Result)

@Result

如何为ErrorCode分配值,使其包含0ErrorCode?如果SP_Validate_User返回的数据集中存在0,我想要该值。如果没有,我希望它包含值SP_Validate_User

我发现了这个问题:

Select columns from result set of stored procedure

这似乎在问我想要什么,但是没有公认的解决方案,并且最高投票的答案不适用,因为if (resultSet.Columns.Count > 3) @Result = 0; else @Result = resultSet["ErrorCode"]; 将返回2个模式中的1个。

我在搜索时发现的所有其他问题都是关于处理数据代码而不是数据库。

基本上,我正在寻找这个伪代码的SQL等价物:

public class Main {

    public static void main(String[] args) {
        // String Change Test
        int[] arr;
        arr = new int[2];
        arr[0] = 1;
        boolean b1 = arr[0] > 1;
        boolean b2 = arr[0] < 1;
        boolean b4 = 0 > arr[0];
        boolean b3 = b4 && b2;
        boolean b5 = b1 || b3;
        if (b5) {
            String s1 = "You have played for " + arr[0] + " hours!";

        }
        else if (arr[0] == 1) {
            String s1 = "You have played for 1 hour!";

        }
        else if (arr[0] == 5) {
            String s1 = "You have not played at all!";
        }
        else {
            String s1 = "Memory Error in arr[0], Are the hours negative? Is it there?";
        }
        System.out.print (s1);
    }
}

1 个答案:

答案 0 :(得分:2)

SP_Validate_User存储过程是两个部分,一个返回错误代码的语句,另一个回退用户,是正确的吗?

如果发生了这种情况,为什么不将IP参数添加到SP_Validate_User proc,然后在该proc中有两个insert语句,一个在满足错误条件时插入,一个在错误条件下插入不满足。

INSERT INTO LoginAudit (IP, Username, Password, AttemptDate, Result)
VALUES (@IP, @UserName, @Password, GETDATE(), someNumber)

SELECT 
someNumber AS ErrorCode, 
otherNumber AS AttemptsRemaining, 
thirdNumber AS LockoutTimeRemaining

RETURN

INSERT INTO LoginAudit (IP, Username, Password, AttemptDate, Result)
VALUES (@IP, @UserName, @Password, GETDATE(), 0)

SELECT * 
FROM Users 
WHERE ...

RETURN