0

我有一个Users包含以下列的表格:

  Userid   Username    Password
     1     Username1   Password1
     2     Username2   Password2
     3     Username3   Password3

还有一张桌子,MappingRoles

 Userid   RoleId
     1       1 (FinanceUser)
     1       2 (Agent)
     1       3 (Client)

我想编写一个应该返回两列的存储过程

IsValidUser
Roles -- of the user

这是我的尝试:

CREATE PROCEDURE  getRoles
    @Username varchar(50), 
    @Password varchar(50) 
AS
BEGIN
    Select * from Users as x inner join MappingRoles as y 
    on x.Userid=y.Userid 
    where x.Userid=(Select Userid from Users 
      where UserName='Username1' and Password='Password1');
END
GO

我期望在此查询中进行优化以提供 2 个返回值:

  • IsUserAuthenticated- (内部Select来自Users

  • 如果它存在,那么所有的值。

任何建议将不胜感激。

4

1 回答 1

1

尝试更简单的连接,删除子查询:

CREATE PROCEDURE  getRoles
    @Username varchar(50), 
    @Password varchar(50) 
AS
BEGIN
    Select 
        * 
    from Users as us 
    inner join MappingRoles mr 
    on us.Userid=mr.Userid 
    where us.UserName=@username and us.Password=@password);
END
GO
于 2013-06-02T19:52:16.673 回答