In this tutorial, you will learn how to use SQL Server DROP VIEW
statement to remove an existing view from the SQL Server Database.
DROP VIEW command is used to delete or drop one or more views from the SQL Server database.
SQL Server DROP VIEW Syntax
To drop a view from the SQL Server database, you need to use the following syntax:
DROP VIEW [IF EXISTS] schema_name.view_name;
In this syntax,
- DROP VIEW – Mandatory. The keyword to drop an existing view from the SQL Server Database.
- [IF EXIST] – Optional. If specified, SQL Server will check if the specified view is present or not in the database. If not specified, SQL Server prompt an error.
IF EXIST
clause prevents SQL Server for prompting error if the specified view does not exist in the database. - schema_name – The name of the schema where the view belongs.
- view_name – The name of the view which you want to remove from the SQL Server database.
In order to remove multiple views in a single statement, you can use the following syntax:
DROP VIEW [IF EXISTS]
schema_name.view_name1,
schema_name.view_name2,
…
;
Here, views are specified with the commas.
SQL Server DROP VIEW example
Suppose, we have below three views created in COMPANY
database.
- vWProductSales
- vWTopThreeProductBySales
- vWTotalSalesByProduct
We will use the above three views to demonstrate DROP VIEW in SQL Server.
1) Removing one view example
The below example shows how to drop vWProductSales view from the COMPANY
database.
DROP VIEW IF EXISTS dbo.VwProductSales;
2) Removing Multiple views example
The following statement removes both dbo.vWTopThreeProductBySales and dbo.vWTotalSalesByProduct views at the same time.
DROP VIEW IF EXISTS
dbo.vWTopThreeProductBySales,
dbo.vWTotalSalesByProduct;
Summary: In this tutorial, you have learned how to remove single and multiple views from the SQL Server Database.