Jwalin Khatri

Easy way to delete all the data from all the tables in database

Posted by: jwalin on: March 3, 2009

 If you want to delete all the records from the all the tables in database you might consider Delete Or Trancate command as both delete all the records from the tables. However it depends on ‘Constraint’ and delete records from one table at a time. But here is the code which delete the records from all the tables in one single shot…

 

CREATE 
AS

 PROCEDURE sp_DeleteAllData
    EXEC sp_MSForEachTable ‘ALTER TABLE ? NOCHECK CONSTRAINT ALL’
    EXEC sp_MSForEachTable ‘ALTER TABLE ? DISABLE TRIGGER ALL’
    EXEC sp_MSForEachTable ‘DELETE FROM ?’
    EXEC sp_MSForEachTable ‘ALTER TABLE ? CHECK CONSTRAINT ALL’
    EXEC sp_MSForEachTable ‘ALTER TABLE ? ENABLE TRIGGER ALL’
    EXEC sp_MSFOREACHTABLE ‘SELECT * FROM ?’

GO

 

Thank you Susan Sales Harkins who exploer this store procedure

Happy Programming !!!!!

 

 

Leave a Reply

Blog Stats

  • 16,646 hits

  • Kingsley Tagbo: Thanks for reading and sharing my article. Have a great like ... Kingsley
  • Bob: Doesn't work with IIS7 (default config on Windows 7 and 2008) because no ADSI anymore.
  • jwalin: Follow the following steps 1]. open you Page in NOTEPAD. 2]. find the tag 3]. Replace the "body" like body oncontextmenu=”return false;" 4]. Sav