hello friend please review below script, i have created temporary table based on your description, and using that table structure i also added select query which is select your desired fields from tables, you need to modified this to your actual data structure..
I have excluded GROUP ROLES in join, if you need to select any role based on that, then you can add that too in selection..
DECLARE @UserTable AS TABLE (
USERID INT,
USERFIRSTNAME VARCHAR(10),
USERLASTNAME VARCHAR(10)
)
DECLARE @GROUPTABLE AS TABLE (
GROUPID INT,
GROUPNAME VARCHAR(10),
GROUPDESCRIPTION VARCHAR(20)
)
DECLARE @USERGROUP AS TABLE (
USERID INT,
GROUPID INT
)
DECLARE @GROUPROLES AS TABLE (
GROUPID INT,
ROLEID INT
)
INSERT INTO @UserTable VALUES(1,'ABC1','XYZ1')
INSERT INTO @UserTable VALUES(2,'ABC2','XYZ2')
INSERT INTO @UserTable VALUES(3,'ABC3','XYZ3')
INSERT INTO @GROUPTABLE VALUES(1,'GROUP1','GROUP1 DESC')
INSERT INTO @GROUPTABLE VALUES(2,'GROUP2','GROUP2 DESC')
INSERT INTO @USERGROUP VALUES (1,1)
INSERT INTO @USERGROUP VALUES (2,2)
INSERT INTO @USERGROUP VALUES (3,1)
INSERT INTO @GROUPROLES VALUES(1,1)
INSERT INTO @GROUPROLES VALUES(2,2)
SELECT UserTable.USERID,UserTable.USERFIRSTNAME,UserTable.USERLASTNAME,GroupTable.GROUPNAME
FROM @UserTable AS UserTable
INNER JOIN @USERGROUP AS UserGroupTable ON UserGroupTable.USERID = UserTable.USERID
INNER JOIN @GROUPTABLE AS GroupTable ON GroupTable.GROUPID = UserGroupTable.GROUPID
WHERE UserTable.USERID = 1
Output is like this...
-- by passing userid as 1
USERID USERFIRSTNAME USERLASTNAME GROUPNAME
1 ABC1 XYZ1 GROUP1
-- by passing userid as 3
USERID USERFIRSTNAME USERLASTNAME GROUPNAME
3 ABC3 XYZ3 GROUP1
-- by passing userid as 2
USERID USERFIRSTNAME USERLASTNAME GROUPNAME
2 ABC2 XYZ2 GROUP2