• I can see some issues with what you have here. It starts with normalization. You really should only have 1 of those UserAccess values per row. I am guessing that only 1 column per row is not null? I would either split this into three tables or add a new column to identify which type of UserAccess it is.

    If you split out the access into 3 tables your query might look something like this.

    SELECT *

    FROM Employees e

    JOIN DepartmentAccess a ON e.DepartmentID = a.DepartmentID

    join OfficeAccess oa on e.DepartmentID = oa.DepartmentID

    join RegionAccess ra on e.DepartmentID = ra.DepartmentID

    WHERE e.EmployeeID = @user-id

    Another option might be to use a UNION ALL for the three types. Something like this.

    SELECT *, 'Dept' as AccessType

    FROM Employees e

    JOIN UserAccess a ON e.DepartmentID = a.DepartmentID

    WHERE a.UserID = @user-id

    UNION ALL

    SELECT *, 'Office'

    FROM Employees e

    JOIN UserAccess a ON e.OfficeID = a.OfficeID

    WHERE a.UserID = @user-id

    UNION ALL

    SELECT *, 'Region'

    FROM Employees e

    JOIN UserAccess a ON e.RegionID = a.RegionID

    WHERE a.UserID = @user-id

    You will notice in all of those I moved the filtering to the where clause instead of on the join. It will work the same but I find it a lot easier to read.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/