Tech Talk A software developer on all things tech, and then some

18May/110

Delete or Truncate All Data in a Database

A quick tip for you all, ever need to delete or truncate all the data from a database? There has been a few times during testing after my database grew, that I needed a quick solution to clear a database. The solution is using the MSForEachTable stored procedure. Simply do the following :

DatabaseName - The name of the database you need to clear out.

To Delete :

USE DatabaseName
EXEC sp_MSForEachTable 'DELETE FROM ?'

To Truncate :

USE DatabaseName
EXEC sp_MSForEachTable 'TRUNCATE TABLE ?'

Happy Coding!

Comments (0) Trackbacks (0)

No comments yet.


Leave a Reply

No trackbacks yet.

%d bloggers like this: