By using this statement in SQL Server:
EXEC sp_msforeachtable 'DROP TABLE ?'
I know it's possible to delete all tables at once.
Is there a similar statement for views? I tried this hoping to be lucky: EXEC sp_msforeachview 'DROP VIEW ?' but it doesn't work!
I wanted a script to drop schema bound views in the correct dependency order, and I wanted it to run on sql azure where
sys.dm_sql_referencing_entities
is not available. I also wanted to be able to view the sql being run before I actually ran it - which you can't do with the script in the answer by @RicNet. So I wrote this recursive query that use the other answers here as a foundationAll answers don't account for constraints between views. This script will take this into account:
This loops to all views and selects the TOP 1 view that is not present in the references systable.
Here you have, no cursor needed:
But what about schema?
The below script will help you if the views are part of schema
Since no one of the scripts I tried from the answers worked correctly in the case of multiple schemas I am including a working one.
--DBNAME, PUT YOU OWN ONE use SIPE_ISU
DECLARE @viewName varchar(500) DECLARE cur CURSOR FOR SELECT sk.name + '.'+so.name FROM sys.objects so inner join sys.schemas sk on sk.schema_id = so.schema_id WHERE type = 'v' OPEN cur FETCH NEXT FROM cur INTO @viewName WHILE @@fetch_status = 0 BEGIN EXEC('DROP VIEW ' + @viewName) FETCH NEXT FROM cur INTO @viewName END CLOSE cur DEALLOCATE cur