August 16, 2011 at 1:42 pm
I have a list of tables that need to truncate and repopulate with data.
But when truncating I got an error that it cannot be truncated because of foreign key constraints.
I found even the foreign key table is empty - no data, I still got the same error.
My question is is it true even the foreign key table is empty, the PK table it refered to cannot be truncated because of this?
Thanks
August 16, 2011 at 1:47 pm
If a foreign key constraint references a table, that table cannot be truncated. Doesn't matter if the other table is empty, doesn't even matter if the constraints are disabled. If they exist at all, no truncation.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
August 16, 2011 at 1:47 pm
a foreign key always prevents truncation.
to do the same thing it requires two separate steps.
instead you have to delete, and then maybe reseed the identity
DELETE FROM MyTable
DBCC CHECKIDENT( [MyTable],RESEED,1) --reset the identity to a value of 1
Lowell
August 16, 2011 at 1:54 pm
Thanks for the answers, that helps a lot
Viewing 4 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply