Delete all rows from all tables SQL server
Have you ever come across the problem to delete all rows from all tables available in a database? One of my colleagues asked me this question that how can we delete all records from multiple tables of a database. So, in this SQL tutorial, we are going to discuss, how we can delete all rows from all tables of a Sql Server Database.
Follow below steps with t-SQL delete all records from all tables from SQL server:
- Disable all constraints first:-
EXEC sys.sp_msforeachtable 'ALTER TABLE ? NOCHECK CONSTRAINT ALL'
- Execute a Delete or truncate operation on each table of the database:-
EXEC sys.sp_msforeachtable 'DELETE FROM ?'
- Enable all constraints on the database:-
EXEC sys.sp_MSForEachTable 'ALTER TABLE ? CHECK CONSTRAINT ALL'
This post first appeared on Asp Dot Net Tricks And Tips, Dot Net Coding Tips, Google Maps API Developer, please read the originial post: here