Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


TRUNCATE / Foreign Key issue


TRUNCATE / Foreign Key issue

Author
Message
Wilfred van Dijk
Wilfred van Dijk
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1079 Visits: 1355
Hi,

I have a database copy script which roughly works as: truncating data, bcp in new data.
I modified this script for a another database which has foreign keys. In order to prevent FK errors, I disable every FK in this database:

alter table {tablename} nocheck constraint {name}



However, truncating a table with even(!) disabled constraints, results in:

Cannot truncate table 'dbo.Activity' because it is being referenced by a FOREIGN KEY constraint.

Changing the truncate table in delete from results in no errors, but leads to a very large logfile. I didn't found any restrictions on truncate in combination with FK (in BOL).

- Is this a known issue (bug?)
- Is there a solution for this? (dropping the FK is not an option)

I use MSSQL 2005 EE SP3

Thanks!

Wilfred
The best things in life are the simple things
SQLZ
SQLZ
SSC Eights!
SSC Eights! (930 reputation)SSC Eights! (930 reputation)SSC Eights! (930 reputation)SSC Eights! (930 reputation)SSC Eights! (930 reputation)SSC Eights! (930 reputation)SSC Eights! (930 reputation)SSC Eights! (930 reputation)

Group: General Forum Members
Points: 930 Visits: 940
This isn't so much of an issue but a feature of the truncate command. Because truncate is not a fully logged operation it doesn't check for referential integrity so if there are any referencing tables the truncate command won't be allowed to run. Disabling the constraint is not enough - you must drop it.

Incidentally, why is dropping the constraint not an option?

Karl
source control for SQL Server
GilaMonster
GilaMonster
SSC Guru
SSC Guru (54K reputation)SSC Guru (54K reputation)SSC Guru (54K reputation)SSC Guru (54K reputation)SSC Guru (54K reputation)SSC Guru (54K reputation)SSC Guru (54K reputation)SSC Guru (54K reputation)

Group: General Forum Members
Points: 54029 Visits: 44625
Wilfred van Dijk (6/11/2009)
I didn't found any restrictions on truncate in combination with FK (in BOL).

- Is this a known issue (bug?)


Not a bug, a documented restriction.

From Books online, the page titled "Truncate Table"
Restrictions
You cannot use TRUNCATE TABLE on tables that:
- Are referenced by a FOREIGN KEY constraint. (You can truncate a table that has a foreign key that references itself.)
- Participate in an indexed view.
- Are published by using transactional replication or merge replication.


Either drop the constraint or use Delete.

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

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search