Roman Rehak

SQL Server and things not related

<July 2008>
SuMoTuWeThFrSa
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789


Navigation

SQL Server Sites

Subscriptions

News

eXTReMe Tracker

Post Categories



Roman's Weekly SQL Server Tip - How to quickly delete all data in the database, version 2 Rated Excellent [5 out of 5].

My tip from last week about deleting all data in the database easily seems to have generated a lot of interest and a good discussion about using DELETE vs. TRUNCATE TABLE. Obviously the truncate command is more efficient but you get an error if you try to run it on a table that's a parent to another table. So I started thinking that maybe there is an easy way to figure out if a table is a parent. In the end, my intuition about the SERVERPROPERTY proved correct - if you pass in 'TableHasForeignRef' as a parameter, SERVERPROPERTY returns 1 if the table has any foreign key references.

So here is is the new version of the script, it uses TRUNCATE TABLE on stand-alone and child tables, or DELETE otherwise:

-- disable referential integrity
EXEC sp_MSForEachTable 'ALTER TABLE ? NOCHECK CONSTRAINT ALL'
GO

EXEC sp_MSForEachTable '
 IF OBJECTPROPERTY(object_id(''?''), ''TableHasForeignRef'') = 1
  DELETE FROM ?
 else
  TRUNCATE TABLE ?
'
GO

-- enable referential integrity again
EXEC sp_MSForEachTable 'ALTER TABLE ? CHECK CONSTRAINT ALL'
GO

posted on Wednesday, March 08, 2006 12:07 PM by Roman


# re: Roman's Weekly SQL Server Tip - How to quickly delete all data in the database, version 2 @ Wednesday, March 08, 2006 3:56 PM

very cool script...

John Warren

# T-SQL Trick for Deleting All Data in Your Database @ Sunday, March 12, 2006 7:07 AM

Anonymous

# re: Roman's Weekly SQL Server Tip - How to quickly delete all data in the database, version 2 @ Monday, March 13, 2006 10:56 AM

The only problem with truncate is its affect on resetting the identity back to its seed value

Scott

# re: Roman's Weekly SQL Server Tip - How to quickly delete all data in the database, version 2 @ Thursday, March 23, 2006 7:50 AM

try your script on 10M+ rows :-) and you may run out of log space
better decision is to replace "delete ?" with

set rowcount 10000
while 1 = 1 begin
delete ?
if @@rowcount = 0 break
end
set rowcount 0

also, enabling all CONSTRAINT may be not correct, if i disable some CONSTRAINT not for task of delete.

locky

# re: Roman's Weekly SQL Server Tip - How to quickly delete all data in the database, version 2 @ Wednesday, April 12, 2006 9:39 PM

Yes locky, you're absolutely right that there could be an issue with large tables. In my previous tip I actually mentioned the issue and recommended doing a batch delete on large tables.

Roman

# re: Roman's Weekly SQL Server Tip - How to quickly delete all data in the database, version 2 @ Wednesday, August 29, 2007 6:51 PM

Thank you Roman!! Just what I was looking for. This just saved me so much time!

Cheers
jase

jase

# re: Roman's Weekly SQL Server Tip - How to quickly delete all data in the database, version 2 @ Tuesday, October 09, 2007 8:50 AM

Awesome, perfect, thanks!!!

Sam

# re: Roman's Weekly SQL Server Tip - How to quickly delete all data in the database, version 2 @ Wednesday, November 07, 2007 1:29 PM

How do I delete all but 10 rows from all tables in the db ?

Thanks

Sonali

# re: Roman's Weekly SQL Server Tip - How to quickly delete all data in the database, version 2 @ Thursday, February 21, 2008 1:16 AM

Thanks Roman,

It is very useful command .............

Cheers,

Chandrashekhar Mahale




Powered by Dot Net Junkies, by Telligent Systems