从同一个表中的存储过程返回多个数据

时间:2012-11-19 08:11:14

标签: sql sql-server-2008 stored-procedures

这是存储过程:

CREATE PROCEDURE [dbo].[sp_Login]
        @Email nvarchar(50),
        @Password nvarchar(50),
        @UserId uniqueidentifier output,
        @UserType int output,
        @IsVerify bit output,
        @IsPremium bit output
    AS
    BEGIN
        SELECT @UserId = UserId FROM Users WHERE Email = @Email AND [Password] = @Password
        SELECT @IsVerify = IsVerify FROM Users WHERE Email = @Email AND [Password] = @Password
        SELECT @UserType = UserType FROM Users WHERE Email = @Email AND [Password] = @Password
        SELECT @IsPremium = IsPremium FROM Users WHERE Email = @Email AND [Password] = @Password
    END

我的问题是:有办法将此参数作为“输出”返回,而不会调用相同的“选择”功能4次。

2 个答案:

答案 0 :(得分:2)

您应该对所有参数使用一个选择,如下所示:

CREATE PROCEDURE [dbo].[sp_Login]
        @Email nvarchar(50),
        @Password nvarchar(50),
        @UserId uniqueidentifier output,
        @UserType int output,
        @IsVerify bit output,
        @IsPremium bit output
    AS
    BEGIN
        SELECT @UserId    = UserId,
               @IsVerify  = IsVerify,
               @UserType  = UserType,
               @IsPremium = IsPremium 
        FROM  Users 
        WHERE Email = @Email 
        AND   [Password] = @Password
    END

答案 1 :(得分:1)

你可以使用像

这样的单一选择语句
        SELECT @UserId = UserId ,
               @IsVerify = IsVerify ,
               @UserType = UserType ,
              @IsPremium = IsPremium 
        FROM Users WHERE Email = @Email AND Password = @Password