Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete all views from Sql Server

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!

like image 493
Sam Avatar asked Jul 27 '12 14:07

Sam


People also ask

How delete all view in SQL?

By using this statement in SQL Server: EXEC sp_msforeachtable 'DROP TABLE ? ' I know it's possible to delete all tables at once.

How do I delete a view in SQL Server?

Using SQL Server Management Studio In Object Explorer, expand the database that contains the view you want to delete, and then expand the Views folder. Right-click the view you want to delete and click Delete. In the Delete Object dialog box, click OK.

How do I drop multiple views in SQL Server?

Here is the syntax to delete multiple views in SQL Server. In the above statement, you need to specify all views that you want to delete, in a comma-separated list. If you don't specify schema name, SQL Server will try to delete view from active schema. drop view products.

Can we delete from view in SQL?

View is like a virtual table which enable us to get information of multiple tables. yes we can insert,update and delete view in sql server.


1 Answers

Here you have, no cursor needed:

DECLARE @sql VARCHAR(MAX) = ''         , @crlf VARCHAR(2) = CHAR(13) + CHAR(10) ;  SELECT @sql = @sql + 'DROP VIEW ' + QUOTENAME(SCHEMA_NAME(schema_id)) + '.' + QUOTENAME(v.name) +';' + @crlf FROM   sys.views v  PRINT @sql; EXEC(@sql); 
like image 51
Yaroslav Avatar answered Sep 19 '22 18:09

Yaroslav