Saubhagya
Saubhagya

Reputation: 1143

Delete all records from all tables of database

Is there any way to delete all records from all tables of a database yet keeping all the constraints.

I used a script available on net but it fails where foreign keys are defined.

Upvotes: 1

Views: 1046

Answers (1)

Praveen
Praveen

Reputation: 36

CREATE PROCEDURE sp_EmplyAllTable
AS
EXEC sp_MSForEachTable ‘ALTER TABLE ? NOCHECK CONSTRAINT ALL’
EXEC sp_MSForEachTable ‘DELETE FROM ?’
EXEC sp_MSForEachTable ‘ALTER TABLE ? CHECK CONSTRAINT ALL’
GO

Upvotes: 2

Related Questions