Mosh Feu
Mosh Feu

Reputation: 29249

return multiple data from stored procedure from the same table

Here Is the stored procedure:

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

My question is: There is way to return this parameters as "output" without call to same "select" function 4 times.

Upvotes: 1

Views: 428

Answers (2)

Robert
Robert

Reputation: 25753

You should use one select for all parameters as below:

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

Upvotes: 2

SRIRAM
SRIRAM

Reputation: 1888

you can use single select statement like

        SELECT @UserId = UserId ,
               @IsVerify = IsVerify ,
               @UserType = UserType ,
              @IsPremium = IsPremium 
        FROM Users WHERE Email = @Email AND Password = @Password

Upvotes: 1

Related Questions