views:

365

answers:

3

delete all rows from all tables in a SQL Server database

+1  A: 

Hai Surajit, http://vadivel.blogspot.com/2006/07/easiest-fastest-way-to-delete-all.html

Pandiya Chendur
and what about triggers ?????????
for triggers refer http://www.sqlservercentral.com/Forums/Topic285605-5-1.aspx#bm822384
Pandiya Chendur
i need a sp to do that....please provide a full sp
+5  A: 

Note that TRUNCATE won't work if you have any referential integrity set.

In that case, this will work:

EXEC sp_MSForEachTable 'ALTER TABLE ? NOCHECK CONSTRAINT ALL'
GO
EXEC sp_MSForEachTable 'DELETE FROM ?'
GO
EXEC sp_MSForEachTable 'ALTER TABLE ? CHECK CONSTRAINT ALL'
GO
Coder 42
nice. good thinking....
Preet Sangha
and what about triggers ?????????
DISABLE TRIGGER ALL ON DATABASE
Coder 42
Actually, that's only for DDL triggers.In which case:EXECP sp_MSForEachTable 'DISABLE TRIGGER ALL ON ?'
Coder 42
EXECP is a typo, of course. Just plain old EXEC.
Coder 42
A: 

You could delete all the rows from all tables using an approach like Rubens suggested, or you could just drop and recreate all the tables. Always a good idea to have the full db creation scripts anyway so that may be the easiest/quickest method.

AdaTheDev
seems OP is concerned about referential integrity and triggers; this case, your got best solution. I'm dropping my answer =)
Rubens Farias
I meant truncating it =)
Rubens Farias