Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
Log in  ::  Register  ::  Not logged in

Get your favorite SSC scripts directly in SSMS with the free SQL Scripts addin. Search for scripts directly from SSMS, and instantly access any saved scripts in your SSC briefcase from the favorites tab.
Download now (direct download link)

Truncating and deleting all tables in a database

By Hunchback,

Sometimes, during test stage, you may need to truncate or delete all your tables in a database, without droping constraints, truncating and recreating constraints (DRI). You can use the TRUNCATE TABLE statement to removes all rows from a table without logging the individual row deletes, but tables referenced by a FOREIGN KEY constraint can not be truncated. For them, you can use the DELETE statement, but you have to do it in the rigth order so you do not delete first the rows from a parent table and get an error because the DRI. This is what this script does.

This script does not check permissions, it supposes that you have sufficient to truncate or delete.

Total article views: 1492 | Views in the last 30 days: 2
Related Articles

Difference between Truncate and Delete

This article explains you the differences between the Truncate and Delete commands in SQL.


Issue with truncation due to FOREIGN KEY constraint.

Issue with truncation due to FOREIGN KEY constraint.


Code For Truncating Table with foreign Key Constraints

This can be used for data warehouses as well as truncating table with foreign key constraints.


Can't Delete Index b/c of Constraint

Can't Delete Index b/c of Constraint


Difference between Delete and Truncate Command

Although the Delete and Truncate Commands logically does the same work of deleting the rows from the...


Join the most active online SQL Server Community

SQL knowledge, delivered daily, free:

Email address:  

You make SSC a better place

As a member of SQLServerCentral, you get free access to loads of fresh content: thousands of articles and SQL scripts, a library of free eBooks, a weekly database news roundup, a great Q & A platform… And it’s our huge, buzzing community of SQL Server Professionals that makes it such a success.

Join us!

Steve Jones

Already a member? Jump in:

Email address:   Password:   Remember me: Forgotten your password?
Steve Jones