In this article I describe how to drop all the Views in a particular database
IntroductionIn this article I describe how to drop all the Views in a particular database. In my previous article I described how to remove all the Stored Procedures in a database. You can visit, Dropping all the stored Procedure in a database in SQL Server.I assume you have a basic knowledge of Views and cursors; for more help you can visit:
Views in SQL Server 2012Cursor in SQL Server 2012First of all we create a tableCreation of Table:create table emp(empId int, empName varchar(15))goinsert into empselect 1,'Deepak'union allselect 2,'Arora'goselect * from empOutput:Now we Create two Views Creation of first View:create view v1asselect * from empgoselect * from v1Output:Creation of second View:create view v2asselect empId from empgoselect * from v2Output:Now we create a Cursor :declare cur_dropView cursorscroll forselect [name] from sysobjects where xtype='v'Now run the following code:open cur_dropViewgoDeclare @ViewName varchar(500)fetch first from cur_dropView into @ViewNamewhile @@fetch_status=0beginExec('drop view ' + @ViewName)fetch next from cur_dropView into @ViewNameendgoclose cur_dropViewgodeallocate cur_dropViewOutput:Now see the Views in the database:select [name] from sysobjects where xtype='v'Output:Summary:In this article I described how to drop all the Views in a database in SQL Server. I hope this article has helped you to understand this topic. Please share if you know more about this. Your feedback and constructive contributions are welcome.
Basics of SQL Server