all groups > sql server programming > may 2007 >
You're in the

sql server programming

group:

Can't Truncate table


Can't Truncate table <->
5/12/2007 10:09:50 AM
sql server programming:
I don't get this.

I have foreign keys set up on a table, which I disabled via:

ALTER TABLE <MyTable> NOCHECK CONSTRAINT ALL

When I view the results of EXEC sp_helpconstraint <MyTable>

I can see that the status_enable for all FK constraints is "Disabled"

However, when I run TRUNCATE Table <MyTable> I get an error message:

"Can't Truncate <MyTable> because it is being referenced by a FOREIGN KEY
Constraint."

As an extra measure, I disabled all constraints on every table (NOCHECK)

Then I performed a generate sql script on all tables and all foreign key
constraints are NOCHECK.
Yet, I still can't truncate MyTable.

Is there something I'm missing?

Re: Can't Truncate table <->
5/12/2007 12:12:55 PM
Ok, then I will do that and recreate them.

But, is this response wrong then? Or am I misunderstanding it?

http://groups.google.com/group/microsoft.public.sqlserver.server/browse_thread/thread/acf1876ea03d2f0e/6432666afd587f44?lnk=st&q=how+to+truncate+table+referenced+by+foreign+key+constraint+sql+server&rnum=2#6432666afd587f44

*********************** Question ********************************
When using truncate table statement on a table with foreign key relationship
will cause an error like,

Server: Msg 4712, Level 16, State 1, Line 1
Cannot truncate table 'purchase' because it is being referenced by a FOREIGN
KEY constraint.

Does anyone know any T-SQL command to disable/enable the constraint(s)?

******************************** Response ********************************

Newsgroups: microsoft.public.sqlserver.server
From: "Kalen Delaney"
<kalen_dela...@hotmail.please_reply_on_public_forums.com>
Date: 2000/08/08
Subject: Re: How to truncate table with foreign key referenced
Reply to author | Forward | Print | Individual message | Show original |
Report this message | Find messages by this author
exec sp_helpconstraint purchase

This will tell which table is referencing purchase


ALTER TABLE <referencing table name> NOCHECK CONSTRAINT ALL


This will disable ALL constraints on that table. Alternatively, you could
look up the actual name of the constraint in the other table.


********************************





[quoted text, click to view]



Re: Can't Truncate table Kalen Delaney
5/12/2007 1:33:23 PM
Hi -

Wow, that was a long time ago!

The answer was incomplete. It looks like I read the post too quickly and was
just answering the specific question that was asked:
"Does anyone know any T-SQL command to disable/enable the constraint(s)?"

I apologize for the confusion. Tom is right, you have to drop the
referencing constraint in order to truncate the table.
--
HTH
Kalen Delaney, SQL Server MVP
www.InsideSQLServer.com
http://sqlblog.com


[quoted text, click to view]

Re: Can't Truncate table Tom Cooper
5/12/2007 2:00:41 PM
You must drop all the constraints that reference a table before you are able
to TRUNCATE it. Disabling them and/or marking them NOCHECK is not enough.

Tom

[quoted text, click to view]

Re: Can't Truncate table Tom Cooper
5/12/2007 2:19:36 PM
Sorry, I should have said: You must drop all the FOREIGN KEY constraints
that reference a table before you are able to TRUNCATE it.

Tom

[quoted text, click to view]

Re: Can't Truncate table <->
5/12/2007 3:27:14 PM
Aah, no problem.

When I first encountered the error message, I ran to google and tried out
the first link I got (yours). Hehe.

Thanks.

[quoted text, click to view]

Re: Can't Truncate table MH
5/12/2007 6:55:22 PM
Does 'DELETE FROM MyTable' work?

MH

[quoted text, click to view]

AddThis Social Bookmark Button