How to delete a SQL Server view?

How to delete a SQL Server view from the database? How to drop a view? For a SQL developer it’s a good practice to have a clean database and remove the unused or useless code. Unlike the tables, the views can be deleted from the database using a simple T-SQL command.

How to delete a SQL Server view from the database?

Let’s consider the Sales by month view created in this article. To drop the view, simply use this SQL command and execute it against the database.

	[dbo].[SalesByMonth] ;

Note that deletion is possible only for user defined views, not the system views using database catalog data.

Check this general informations about the views on official SQL Server website.

To go further on the method and SQL code management, it’s a good practice to check the dependencies of the view before deleting it.

For example, let’s consider the first table Sales that is used by the the view dbo.SalesByMonth. Let’s consider now a third object, a view that display the Sales with a Year-To-Date vision: dbo.SalesByMonthYTD. If you drop the first view then the YTD one will not work anymore.

Be the first to comment

Leave a Reply

Your email address will not be published.