Wednesday, 14 September 2016

SQL Query to delete all view in SQL Server

Query to delete all view in SQL Server
Description:

In this example we explain that how to delete all views in SQL server or how to remove all View in SQL Server.below is the SQL Query to remove all the views form SQL Server.

Here we use the Cursor to loop each view one by one and drop or delete this view from SQL Server.
 Query:

DECLARE @view_Name varchar(1000)

DECLARE cursor_views cursor
FOR SELECT [name] FROM sys.objects WHERE TYPE = 'v'

OPEN cursor_views
FETCH NEXT FROM cursor_views INTO @view_Name
while @@fetch_status = 0
BEGIN
    exec('DROP VIEW ' + @view_Name)
    print 'Deleted View are -> '+ @view_Name
    FETCH NEXT  FROM cursor_views INTO @view_Name
END
CLOSE cursor_views

DEALLOCATE cursor_views
This entry was posted in :

0 comments:

Post a Comment