Sometimes, there is a case, when we need to remove all tables, stored procedure, views and triggers completely from the database. If you have around 100 tables, stored procedure and views in your database, to remove these, completely from database became a tedious task. In this article, I would like to share the script by which you can remove tables, stored procedure, views and triggers completely from database.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 proceduresDeclare @procName varchar(500)Declare cur Cursor For Select [name] From sys.objects where type = 'p'Open curFetch Next From cur Into @procNameWhile @@fetch_status = 0BeginExec('drop procedure ' + @procName)Fetch Next From cur Into @procNameEndClose curDeallocate curRemove all Views
-- drop all user defined viewsDeclare @viewName varchar(500)Declare cur Cursor For Select [name] From sys.objects where type = 'v'Open curFetch Next From cur Into @viewNameWhile @@fetch_status = 0BeginExec('drop view ' + @viewName)Fetch Next From cur Into @viewNameEndClose curDeallocate curRemove all Triggers
-- drop all user defined triggersDeclare @trgName varchar(500)Declare cur Cursor For Select [name] From sys.objects where type = 'tr'Open curFetch Next From cur Into @trgNameWhile @@fetch_status = 0BeginExec('drop trigger ' + @trgName)Fetch Next From cur Into @trgNameEndClose curDeallocate cur
0 comments:
Post a Comment