Views in SQL Server

Using a query, a view creates a virtual table whose contents are known. In other terms, a view is a hypothetical table built from a SQL statement’s result set.

A view is made up of a number of named columns and rows of data, similar to a table. It contains fields from one or more actual database tables.

A view does not exist as a set of data values recorded in a database until it is indexed.

A view can be defined with SQL statements and functions to present data as though it were drawn from a single table.

Consider the below example:

USE TestDatabase;
GO
— Create a view.
CREATE VIEW vwEmployeeDetails
AS
SELECT CONCAT(e.FirstName, e.MiddleName, e.LastName) AS EmployeeName, e.HireDate, e.Status, D.DName
FROM Emp e
JOIN Dept D ON e.DeptID = D.DeptID;

— Create a similar view by adding a WHERE clause to fetch the active employees.
CREATE VIEW vwActiveEmployeeDetails
AS
SELECT CONCACT(e.FirstName, e.MiddleName, e.LastName) AS EmployeeName, e.HireDate, e.Status, D.DName
FROM Emp e
JOIN Dept D ON e.DeptID = D.DeptID
WHERE e.Status = ‘Active’
GO

Hope you find this article helpful.

One comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s