how to delete all tables data in sql server

1
How to delete all tables data in SQL Server SQL SERVER HOW TO DELETE ALL TABLES DATA By using below script we can able to delete all tables data in SQL SERVER with having any issues. In the script I have followed below steps Disable all constraints on all tables. Due to that we can delete all tables without having any constraints issues by using system stored procedure sp_MSForEachTable Disable all TRIGGERs on all tables DELETE all tables data If any tables having identity, we need to set identity to 0 Once we delete all tables data , we will enable all CONSTRAINTS Once we delete all tables data , we will enable all TRIGGERs Cautious Before you execute this script and always use statement to ensure that your executing in correct database EXEC SP_MSFOREACHTABLE 'ALTER TABLE ? NOCHECK CONSTRAINT ALL' EXEC SP_MSFOREACHTABLE 'ALTER TABLE ? DISABLE TRIGGER ALL' EXEC SP_MSFOREACHTABLE 'DELETE FROM ?' EXEC SP_MSFOREACHTABLE 'IF OBJECTPROPERTY(OBJECT_ID(''?''), ''TABLEHASIDENTITY'') = 1 DBCC CHECKIDENT (''?'', RESEED, 0)' EXEC SP_MSFOREACHTABLE 'ALTER TABLE ? CHECK CONSTRAINT ALL' EXEC SP_MSFOREACHTABLE 'ALTER TABLE ? ENABLE TRIGGER ALL'

Upload: kiran-reddy

Post on 14-Sep-2015

23 views

Category:

Documents


3 download

DESCRIPTION

How to delete all tables data in SQL Server

TRANSCRIPT

How to delete all tables data in SQL ServerSQL SERVER HOW TO DELETE ALL TABLES DATA

By using below script we can able to delete all tables data in SQL SERVER with having any issues. In the script I have followed below steps Disable all constraints on all tables. Due to that we can delete all tables without having any constraints issues by using system stored procedure sp_MSForEachTable Disable all TRIGGERs on all tables DELETE all tables data If any tables having identity, we need to set identity to 0 Once we delete all tables data , we will enable all CONSTRAINTS Once we delete all tables data , we will enable all TRIGGERsCautious Before you execute this script and always use statement to ensure that your executing in correct database EXEC SP_MSFOREACHTABLE 'ALTER TABLE ? NOCHECK CONSTRAINT ALL'EXEC SP_MSFOREACHTABLE 'ALTER TABLE ? DISABLE TRIGGER ALL'EXEC SP_MSFOREACHTABLE 'DELETE FROM ?'EXEC SP_MSFOREACHTABLE 'IF OBJECTPROPERTY(OBJECT_ID(''?''), ''TABLEHASIDENTITY'') = 1 DBCC CHECKIDENT (''?'', RESEED, 0)'EXEC SP_MSFOREACHTABLE 'ALTER TABLE ? CHECK CONSTRAINT ALL'EXEC SP_MSFOREACHTABLE 'ALTER TABLE ? ENABLE TRIGGER ALL'