从sql存储过程返回一个整数值

时间:2018-01-11 10:20:57

标签: sql-server integer return procedure

我有一个存储过程:

public void GenerateRandNr(int total)
    {
        var rnd = new Random();
        var nr1 = rnd.Next(0, total);
        var nr2 = rnd.Next(0, total - nr1);
        var nr3 = rnd.Next(0, total - nr1 - nr2);
        var nr4 = rnd.Next(0, total - nr1 - nr2 - nr3);
        var nr5 = total - nr1 - nr2 - nr3 - nr4;
    }

问题是它没有返回正确的整数值

这是调用例程(VB)

ALTER PROCEDURE [dbo].[TG_CheckUserLoginStatus]
    -- Add the parameters for the stored procedure here
    (
        @UserName nvarchar(100),
        @UserStatus INT output
    )

AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Does the user exist
    If  NOT (EXISTS (SELECT name FROM sys.sql_logins WHERE (name = @UserName)))
        SELECT @UserStatus = 0;

    ELSE    If EXISTS (SELECT name FROM sys.sql_logins WHERE ((name = @UserName) AND 
                    ((LOGINPROPERTY(name, 'IsExpired')  = 1) OR ((LOGINPROPERTY(name, 'IsMustChange') = 1)))))
                SELECT @UserStatus = 1;
            ELSE
                SELECT @UserStatus = 2;


END

收到的值与我用sql management studio执行时获得的值不一致。

我在这里缺少什么?

2 个答案:

答案 0 :(得分:0)

在sql脚本

中的END之前添加SELECT @UserStatus;

答案 1 :(得分:0)

ExecuteScalar获取结果集第一行的第一列。您正在通过输出参数返回值,因此您需要阅读它。

而不是使用

mRetVal = CType(oCmd.ExecuteScalar, Integer)

使用

oCmd.ExecuteNonQuery()
mRetVal = CType(op.Value, Integer)