Altering a View
Learn how to modify an existing SQL Server View using the ALTER VIEW statement with practical examples.
What is ALTER VIEW?
The ALTER VIEW statement is used to modify the definition of an existing SQL Server View.
When the structure or SELECT query of a View needs to be changed, we can use ALTER VIEW instead of dropping the View and creating it again.
The ALTER VIEW statement changes the View definition while keeping the View object in the database.
ALTER VIEW Syntax
ALTER VIEW ViewName
AS
SELECT column1, column2
FROM TableName
WHERE condition;
Syntax Explanation
- ALTER VIEW specifies that an existing View will be modified.
- ViewName is the name of the existing View.
- AS separates the View name from the SELECT query.
- The SELECT statement defines the new View definition.
Step 1: Create an Existing View
First, suppose we already have a View named EmployeeDetailsView.
CREATE VIEW EmployeeDetailsView
AS
SELECT
EmployeeId,
EmployeeName,
Department
FROM Employees;
This View currently displays EmployeeId, EmployeeName, and Department.
Checking the Existing View
We can retrieve the data from the View using SELECT:
SELECT *
FROM EmployeeDetailsView;
Example 1: Adding a Column
Suppose we now want to include the employee's salary in the View.
We can modify the existing View using ALTER VIEW:
ALTER VIEW EmployeeDetailsView
AS
SELECT
EmployeeId,
EmployeeName,
Department,
Salary
FROM Employees;
The View now returns the Salary column along with the previously selected columns.
Example 2: Adding a WHERE Condition
We can also change the View so that it returns only employees from the IT department.
ALTER VIEW EmployeeDetailsView
AS
SELECT
EmployeeId,
EmployeeName,
Department,
Salary
FROM Employees
WHERE Department = 'IT';
After altering the View, only IT department employees will be returned.
Example 3: Changing the View Query
The SELECT query inside an existing View can be changed completely using ALTER VIEW.
ALTER VIEW EmployeeDetailsView
AS
SELECT
EmployeeName,
Salary
FROM Employees
WHERE Salary > 50000;
The View now displays only EmployeeName and Salary for employees whose salary is greater than 50000.
Verifying the Altered View
After using ALTER VIEW, execute the View to verify the new definition.
SELECT *
FROM EmployeeDetailsView;
The result will now be based on the new SELECT query defined by ALTER VIEW.
CREATE VIEW vs ALTER VIEW
| CREATE VIEW | ALTER VIEW |
|---|---|
| Creates a new View. | Modifies an existing View. |
| Used when the View does not exist. | Used when the View already exists. |
| Creates the View object. | Changes the definition of the existing View object. |
Important Points
- ALTER VIEW is used for an existing View.
- It changes the SELECT definition of the View.
- The View name remains the same.
- The underlying tables are not automatically changed just because the View definition is altered.
- Always test the altered View after making changes.
Best Practices
- Check the existing View definition before altering it.
- Test the new SELECT query separately before using ALTER VIEW.
- Make sure applications depending on the View are not affected by unexpected column changes.
- Use meaningful and consistent View names.
- Verify the View after altering it.
Summary
The ALTER VIEW statement is used to modify the definition of an existing SQL Server View. We can change the SELECT query, add or remove selected columns, and modify filtering conditions without dropping the View object first.