T-SQL存储过程

时间:2013-06-02 18:59:32

标签: .net sql sql-server tsql stored-procedures

我有一个表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

  • 如果它存在则所有值。

任何建议都将受到高度赞赏。

1 个答案:

答案 0 :(得分: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