T-SQL Tutorial T-SQL Advanced Database Management T-SQL References

T-SQL - DROP VIEW Keyword



The T-SQL (Transact-SQL) DROP VIEW statement is used to delete a T-SQL VIEW. A T-SQL VIEW is a virtual table created based on the T-SQL statement. A view contains rows and columns just like a normal table. All T-SQL functions, WHERE, HAVING and JOINs statements can be used to create a T-SQL VIEW. Finally, to delete a T-SQL VIEW, DROP VIEW keyword is used.

Syntax

The syntax for using DELETE VIEW statement in T-SQL (Transact-SQL) is given below:

DROP VIEW [IF EXISTS] view_name;

The IF EXISTS is an optional parameter that conditionally drops view only if it exists on the database. If a view is deleted which does not exist, it will raise an error.

Example:

Consider a database table called Employee with the following records:

EmpIDNameCityAgeSalary
1JohnLondon253000
2MarryNew York242750
3JoParis272800
4KimAmsterdam303100
5RameshNew Delhi283000
6HuangBeijing282800

The below mentioned T-SQL statement is used to create a view on Employee table which contains all records of employee whose salary is greater than 2800.

CREATE VIEW Employee_Salary_GT_2800 AS
SELECT * FROM Employee WHERE Salary > 2800;

After creating the VIEW, it can be used as mentioned below:

SELECT * FROM Employee_Salary_GT_2800;

This will produce the result as shown below:

EmpIDNameCityAgeSalary
1JohnLondon253000
4KimAmsterdam303100
5RameshNew Delhi283000

The DROP VIEW keyword is used to delete a view.

DROP VIEW Employee_Salary_GT_2800;

❮ T-SQL Keywords