Hi All
I want to TRUNCATE all tables in SQL Server database, they have FK Constraints.
I have executed the following SP's. First took a list of Constraints using this
SELECT (CASE
WHEN OBJECTPROPERTY(CONSTID, 'CNSTISDISABLED') = 0 THEN 'ENABLED'
ELSE 'DISABLED'
END) AS STATUS,
OBJECT_NAME(CONSTID) AS CONSTRAINT_NAME,
OBJECT_NAME(FKEYID) AS TABLE_NAME,
COL_NAME(FKEYID, FKEY) AS COLUMN_NAME,
OBJECT_NAME(RKEYID) AS REFERENCED_TABLE_NAME,
COL_NAME(RKEYID, RKEY) AS REFERENCED_COLUMN_NAME
FROM SYSFOREIGNKEYS
ORDER BY TABLE_NAME, CONSTRAINT_NAME,REFERENCED_TABLE_NAME, KEYNO
USE database_name;
GO
EXEC sp_MSforeachtable ‘ALTER TABLE ? NOCHECK CONSTRAINT all’;
I ran the first SQL and everything shows disabled, but when I run the below Query
EXEC sp_MSForEachTable 'TRUNCATE TABLE?'
I still get the same FK Constraint message, please advice, I have over 180 tables.
Thanks