Truncate all tables in a SQL Server database

por | 23 abril, 2025
USE dbexample;
GO

-- Disable all constraints
EXEC sp_msforeachtable 'ALTER TABLE ? NOCHECK CONSTRAINT ALL';

-- Disable triggers (optional)
-- EXEC sp_msforeachtable 'DISABLE TRIGGER ALL ON ?';

-- Truncate all tables
EXEC sp_msforeachtable 'TRUNCATE TABLE ?';

-- Enable all constraints
EXEC sp_msforeachtable 'ALTER TABLE ? WITH CHECK CHECK CONSTRAINT ALL';

-- Enable triggers (optional)
-- EXEC sp_msforeachtable 'ENABLE TRIGGER ALL ON ?';