Forum Discussion
hemantinsnapsys
Aug 19, 2023Copper Contributor
Issue Truncating Table with Foreign Key Constraints in Microsoft SQL Server 2022
Hi everyone, I'm currently working with Microsoft SQL Server 2022, and I'm encountering an issue when trying to truncate a table. The error message I'm receiving is as follows: "Cannot truncate ...
Mike_Lemay
May 27, 2025Copper Contributor
I haven't used this lately but it's worked for me in the past.
-- disable all constraints
EXEC sp_MSForEachTable "ALTER TABLE ? NOCHECK CONSTRAINT all"
-- delete data in all tables
EXEC sp_MSForEachTable "DELETE FROM ?"
-- enable all constraints
exec sp_MSForEachTable "ALTER TABLE ? WITH CHECK CHECK CONSTRAINT all"
--Reseed identity fields
EXEC sp_MSForEachTable "DBCC CHECKIDENT ( '?', RESEED, 0)"