Embed presentation
Download to read offline
![EXAMPLE OF SQL QUERIES
Remove all Tables
-- drop all user defined tables
EXEC sp_MSforeachtable @command1 = "DROP TABLE ?"
Remove all User-defined Stored Procedures
-- drop all user defined stored procedures
Declare @procName varchar(500)
Declare cur Cursor For Select [name] From sys.objects where type = 'p'
Open cur
Fetch Next From cur Into @procName
While @@fetch_status = 0
Begin
Exec('drop procedure ' + @procName)
Fetch Next From cur Into @procName
End
Close cur
Deallocate cur
Remove all Views
-- drop all user defined views
Declare @viewName varchar(500)
Declare cur Cursor For Select [name] From sys.objects 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](https://image.slidesharecdn.com/examplesofsqlqueries-160318190055/75/Examples-of-sql-queries-1-2048.jpg)
![Remove all Triggers
-- drop all user defined triggers
Declare @trgName varchar(500)
Declare cur Cursor For Select [name] From sys.objects where type = 'tr'
Open cur
Fetch Next From cur Into @trgName
While @@fetch_status = 0
Begin
Exec('drop trigger ' + @trgName)
Fetch Next From cur Into @trgName
End
Close cur
Deallocate cur](https://image.slidesharecdn.com/examplesofsqlqueries-160318190055/85/Examples-of-sql-queries-2-320.jpg)

This document provides SQL queries to remove different database objects from a SQL Server database. The queries remove all tables using sp_MSforeachtable, remove all stored procedures by dropping each one in a loop, remove all views by dropping each one in another loop, and finally remove all triggers by dropping each one in a final loop.
![EXAMPLE OF SQL QUERIES
Remove all Tables
-- drop all user defined tables
EXEC sp_MSforeachtable @command1 = "DROP TABLE ?"
Remove all User-defined Stored Procedures
-- drop all user defined stored procedures
Declare @procName varchar(500)
Declare cur Cursor For Select [name] From sys.objects where type = 'p'
Open cur
Fetch Next From cur Into @procName
While @@fetch_status = 0
Begin
Exec('drop procedure ' + @procName)
Fetch Next From cur Into @procName
End
Close cur
Deallocate cur
Remove all Views
-- drop all user defined views
Declare @viewName varchar(500)
Declare cur Cursor For Select [name] From sys.objects 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](https://image.slidesharecdn.com/examplesofsqlqueries-160318190055/75/Examples-of-sql-queries-1-2048.jpg)
![Remove all Triggers
-- drop all user defined triggers
Declare @trgName varchar(500)
Declare cur Cursor For Select [name] From sys.objects where type = 'tr'
Open cur
Fetch Next From cur Into @trgName
While @@fetch_status = 0
Begin
Exec('drop trigger ' + @trgName)
Fetch Next From cur Into @trgName
End
Close cur
Deallocate cur](https://image.slidesharecdn.com/examplesofsqlqueries-160318190055/85/Examples-of-sql-queries-2-320.jpg)