Вы можете попробовать это:
CREATE TABLE #EMPLOYEE
(
EMPLOYEEID INT NOT NULL
, NAME VARCHAR(100) NOT NULL
)
INSERT INTO #EMPLOYEE VALUES
(1, 'John')
, (2, 'Lisa')
, (3, 'Emilia')
, (4, 'Jacob')
, (5, 'Henry')
CREATE TABLE #ROLE
(
ROLEID INT NOT NULL
, ROLENAME VARCHAR(100) NOT NULL
)
INSERT INTO #ROLE VALUES
(1, 'helper')
, (2, 'viewer')
, (3, 'designer')
CREATE TABLE #EMPLOYEE_ROLE
(
EMPLOYEEID INT NOT NULL
, ROLEID INT NOT NULL
)
INSERT INTO #EMPLOYEE_ROLE VALUES
(1, 1)
, (1, 2)
, (1, 3)
, (2, 2)
, (2, 3)
, (3, 1)
, (3, 2)
, (3, 3)
, (4, 1)
, (5, 1)
SELECT #EMPLOYEE.EMPLOYEEID
, #EMPLOYEE.NAME
FROM #EMPLOYEE
INNER JOIN #EMPLOYEE_ROLE
ON #EMPLOYEE.EMPLOYEEID = #EMPLOYEE_ROLE.EMPLOYEEID
WHERE #EMPLOYEE_ROLE.ROLEID IN
(
--EMPLOYEE-ROLES
SELECT DISTINCT ER.ROLEID
FROM #EMPLOYEE E
INNER JOIN #EMPLOYEE_ROLE ER
ON E.EMPLOYEEID = ER.EMPLOYEEID
WHERE E.EMPLOYEEID = 5 --HERE IS THE PARAMETER
)
GROUP BY #EMPLOYEE.EMPLOYEEID
, #EMPLOYEE.NAME
Если вы поставите 5 в предложении WHERE, у вас будет
3 Emilia
5 Henry
4 Jacob
1 John
Надеюсь, это поможет