My Blog List

LOVE IS LIFE

#htmlcaption1 #htmlcaption2 Stay Connected

Pages

Thursday, June 27, 2013

Clean\delete\truncate full database having key constraints

Clean\delete\truncate full database having key constraints

-- Disable the Referential Integrity
EXEC sp_MSForEachTable 'ALTER TABLE ? NOCHECK CONSTRAINT ALL'
GO

EXEC sp_MSForEachTable 'DELETE FROM ?'
GO

-- Enable Referential Integrity again
EXEC sp_MSForEachTable 'ALTER TABLE ? CHECK CONSTRAINT ALL'

GO

2 comments:

  1. I dnt rely understand..wud u pls expln me?

    ReplyDelete
    Replies
    1. In Sql server u have some in built Stored procedures like sp_MSForEachTable... so first i am making all constraints inactive
      then calling the delete statement to delete all data from all tables and finally making all constraints back to previous state that is active state....
      Let me know if you have any other queries

      Delete