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: 1479 | Views in the last 30 days: 2
 
Related Articles
ARTICLE

Difference between Truncate and Delete

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

FORUM

Issue with truncation due to FOREIGN KEY constraint.

Issue with truncation due to FOREIGN KEY constraint.

FORUM

Can't Delete Index b/c of Constraint

Can't Delete Index b/c of Constraint

BLOG

Difference between Delete and Truncate Command

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

FORUM

Discovering Foreign Key Constraints

Can't delete data until all constraints are dropped

Tags
miscellaneous    
 
Contribute

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
Editor, SQLServerCentral.com

Already a member? Jump in:

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