keyongtech


  keyongtech > sqlserver.* > sqlserver.programming > 05/2007

 #1  
05-12-07, 06:09 PM
John
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?
 #2  
05-12-07, 06:55 PM
MH
Does 'DELETE FROM MyTable' work?

MH

<-> wrote in message news:4628
[..]
 #3  
05-12-07, 07:00 PM
Tom Cooper
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

<-> wrote in message news:4628
[..]
 #4  
05-12-07, 07:19 PM
Tom Cooper
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

"Tom Cooper" <tomcooper> wrote in message
news:3496
[..]
 #5  
05-12-07, 08:12 PM
John
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/micro...32666afd587f44

*********************** 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>
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.


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





"Tom Cooper" <tomcooper> wrote in message
news:3996
[..]
 #6  
05-12-07, 09:33 PM
Kalen Delaney
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.
 #7  
05-12-07, 11:27 PM
John
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.

"Kalen Delaney" <replies> wrote in message
news:a492
[..]
Similar Threads
Grant permission to table for truncate table

hi, we have object-level user security. That user need permission for truncate one table. I can't set the user as sysadmin or DBO. any way that I could grant the user to have...

Truncate table

I'm trying to use a table type variable in my stored procedure: DECLARE @Temp3 table ( RecordID int IDENTITY (1, 1) Primary key NOT NULL , FAccountID int, [Name]...

truncate table

Hi, What privileges need to be granted to a user before he can truncate a table?

truncate table

Hi all, Is there a way to truncate a table from a linked DB? Truncate table server.DB.DBO.tablename produces an error. too many parts. TIA, Joe

Truncate Table vs. other?

Hey all, I am trying to research information on the best(?) method(s) for a nightly data repopulate. This situation is this: We have a packaged/purchased system one of...


All times are GMT. The time now is 11:28 PM. | Privacy Policy