CREATE VIEW EmployeesData
AS
(SELECT E.EmployeeID, D.DepartmentName, E.EmployeeLastName, E.EmployeeFirstName, C.CategoryName
FROM EmployeeSalary E
INNER JOIN Department D ON (E.DepartmentID = D.DepartmentID)
INNER JOIN Category C ON (E.CategoryID = C.CategoryID))
GO
GRANT SELECT ON EmployeesData TO [MyDomain\Employees]
GO
Do not forget that users doesn't need to have permissions on any of the above tables because of the ownership chaining I referred in my
second article.
-- How many people are working in IT department
SELECT COUNT(*)
FROM EmployeesData
WHERE DepartmentName = 'IT'
-- Who are the people working in IT department
SELECT *
FROM EmployeesData
WHERE DepartmentName = 'IT'
Continuing with our company's Intranet we want to provide a web page with employee's salary receipt so each employee can only see their own information. With the View created in my
first article we can easily join with this one created here and we will have all the necessary information:
CREATE VIEW EmployeeSalary AS
(SELECT *
FROM EmployessData E
INNER JOIN AllSalaries S ON (E.EmployeeID = S.EmployeeID))
Don't forget that AllSalaries's View it's already limiting the data by the EmployeeID so each employee will only see his relevant information.
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (0)