CREATE VIEW view_EmployeeInfo
AS
SELECT EmployeeID,
FirstName,
LastName,
HireDate
FROM Employee
GO
Rows from views can be selected much like tables:
SELECT FirstName
FROM view_EmployeeInfo
You may also create a view with a calculated column. We can modify the view above as follows by adding a calculated column:
CREATE VIEW view_EmployeeReport
AS
SELECT EmployeeID,
FirstName,
LastName,
Coalesce(FirstName,'') + ' ' + Coalesce(LastName,'') as FullName,
HireDate
FROM Employee
GO
This view adds an additional column that will appear when you SELECT
rows from it. The values in this additional column will be dependent on the fields FirstName
and LastName
in the table Employee
and will automatically update behind-the-scenes when those fields are updated.