Drop all tables in a database with simple SQL

sometimes you just want to drop the tables and leave the DB

This is a very simple and useful bit of SQL shown to me by a colleague. Often times, there are a bunch of foreign keys preventing you form just dorpping tables - it can be a pain to figure out what order you need to delete them in in order to do it correctly. Just run this until all the tables are gone:
exec sp_MSforeachtable "DROP TABLE ? PRINT '? dropped' "

You may see errors, that is normal (you are simply being notified of the foreign keys and such. All the tables will be gone when you no longer see and error messages.
Comments are closed