Saturday, August 16, 2014

A script to drop all tables and their content, views and stored procedures in MS SQL

Occasionally as a developer you may encounter a case where you want to completely remove all tables, views and stored procedures in a database without removing the system tables, system views and system stored procedures or without having to delete the database as a whole and recreating it.

One way is to right-click on each table one by one and try to delete them, only to find out that a foreign key constraint prevents you from doing so, at which time you have to try and figure out how each table relates to the other, and remove them in the correct sequence. If you have a few hundred or even thousand database objects this could take a long time!

Fear not, because we finally have a solution for this! The script below will take care of this for you in one shot and give you a clean database to work with.


No comments: