Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Outer Join problem

Author  Topic 

rednelo
Starting Member

4 Posts

Posted - 2008-09-09 : 10:53:44
I'm writing a simple program to assign and delete roles to users. What I need to see is a listing of all the roles and identify which roles are assigned to a given user.
I'm having a little difficulty creating a sproc that provides the data. This is what I have so far:
 

--To explain what I need I created this view. It provides a list of all the roles to which the current user is assigned.

[view_MaintainUserInRole_1]
SELECT TOP (100) PERCENT aspnet_UsersInRoles.UserId, aspnet_Roles.RoleId, aspnet_Roles.RoleName
FROM aspnet_UsersInRoles INNER JOIN
aspnet_Roles ON aspnet_UsersInRoles.RoleId = aspnet_Roles.RoleId
WHERE (aspnet_UsersInRoles.UserId = '24989ed4-b4e7-4ca3-baed-xxxxxxxxxxxx')
ORDER BY aspnet_Roles.RoleName

--The following correctly yeilds the following 3 results:

24989ED4-B4E7-4CA3-BAED-xxxxxxxxxxxx 59D0FD1B-F452-48E6-8A53-677CD803910C Administration
24989ED4-B4E7-4CA3-BAED-xxxxxxxxxxxx 3A848D83-C401-4B05-8052-3D29F956877A Maintinance
24989ED4-B4E7-4CA3-BAED-xxxxxxxxxxxx F020038F-67C4-43FE-9559-7DFEDFECEC54 Reports

--When I create query using this MaintainUsersIn Role_1 view and add an outer join on the aspnet_Roles table,
--it provides me with a list of all the roles assigned to the user AND those available for selection with a NUll
--value for the UserID. This is exactly what I want.

SELECT view_MaintainUserInRole_1.UserId, aspnet_Roles.RoleId, aspnet_Roles.RoleName
FROM view_MaintainUserInRole_1 RIGHT OUTER JOIN aspnet_Roles
ON view_MaintainUserInRole_1.RoleId = aspnet_Roles.RoleId

--The following correctly yeilds the following 4 records:

24989ED4-B4E7-4CA3-BAED-xxxxxxxxxxxx 59D0FD1B-F452-48E6-8A53-677CD803910C Administration
24989ED4-B4E7-4CA3-BAED-xxxxxxxxxxxx 3A848D83-C401-4B05-8052-3D29F956877A Maintinance
24989ED4-B4E7-4CA3-BAED-xxxxxxxxxxxx F020038F-67C4-43FE-9559-7DFEDFECEC54 Reports
NULL 1F65737A-7EEB-47F3-9B86-8B570F944F7D Customer

--The problem is that the UserID needs to be a variable and I cannot figure out how to do the second part.

--I tried the following and even switched the right/left but always get 3 the records assigned to the user...

SELECT aspnet_UsersInRoles.UserId, aspnet_Roles.RoleId, aspnet_Roles.RoleName
FROM aspnet_UsersInRoles Right Outer Join aspnet_Roles ON aspnet_UsersInRoles.RoleId = aspnet_Roles.RoleId
WHERE (aspnet_UsersInRoles.UserId = @UserID Or aspnet_UsersInRoles.UserId Is Null)
ORDER BY aspnet_Roles.RoleName

SELECT aspnet_UsersInRoles.UserId, aspnet_Roles.RoleId, aspnet_Roles.RoleName
FROM aspnet_UsersInRoles Right Outer Join aspnet_Roles ON aspnet_UsersInRoles.RoleId = aspnet_Roles.RoleId
WHERE (aspnet_UsersInRoles.UserId = @UserID)
Group By Allaspnet_UsersInRoles.UserId, aspnet_Roles.RoleId, aspnet_Roles.RoleName
ORDER BY aspnet_Roles.RoleName

--Any help would be appreciated.

sakets_2000
Master Smack Fu Yak Hacker

1472 Posts

Posted - 2008-09-09 : 11:23:26
Hope I am understanding your problem rightly. Guess, This is what you need.

SELECT view_MaintainUserInRole_1.UserId, aspnet_Roles.RoleId, aspnet_Roles.RoleName
FROM view_MaintainUserInRole_1 RIGHT OUTER JOIN aspnet_Roles
ON view_MaintainUserInRole_1.RoleId = aspnet_Roles.RoleId and aspnet_UsersInRoles.UserId = @UserID
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-09-09 : 11:32:02
[code]SELECT y.UserId,
x.RoleId,
x.RoleName
FROM aspnet_Roles AS x
LEFT JOIN view_MaintainUserInRole_1 AS y ON y.RoleId = x.RoleId
AND y.UserID = @UserID[/code]


E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

rednelo
Starting Member

4 Posts

Posted - 2008-09-09 : 11:34:00
thank-you. It gives me the following error:

Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "aspnet_UsersInRoles.UserId" could not be bound.
Go to Top of Page

rednelo
Starting Member

4 Posts

Posted - 2008-09-09 : 11:41:17
This works perfectly. Thank-you.

I've never seen the way you did this way before. I'm scratching my head... I guess I need to convert it into something I can understand. But it does work. Thanks!


Go to Top of Page
   

- Advertisement -