ALTER VIEW in SQL Server

In contrast to MySQL and a few other RDBMSs, Microsoft SQL Server allows us to change the VIEW.

The VIEW can be modified using ALTER statement. However, you cannot change particular columns in a view because the modified view overrides the original view. The result set of a SELECT query or a UNION of such queries forms the basis of a view, which is a virtual 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;

— Modify the view by adding a WHERE clause to fetch the active employees.
ALTER VIEW vwEmployeeDetails
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 )

Twitter picture

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

Facebook photo

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

Connecting to %s