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!

15Mar/110

Drop, Truncate, and Delete : The Difference


If you wanted to get rid of all the data in a table, then you could you either of the three. However there are certain situations which using a different one of these is the best route to go.

   
%d bloggers like this: