Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
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: 1495 | 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.

SCRIPT

Code For Truncating Table with foreign Key Constraints

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

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...

Tags
miscellaneous    
 
Contribute