Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 error msg when doing a truncate

Author  Topic 

eurob
Posting Yak Master

100 Posts

Posted - 2006-11-01 : 15:53:48
I try to truncate a table (mytable) but get the msg that it cannot be truncated due to 'being referenced by a FOREIGN KEY constraint'. I removed all constraints in all tables that have any reference to 'mytable', but it keeps coming up with this msg. I might have missed one, sql server, however does not say which table are referenced though. Is there a statement that shows all foreign key constraints associated with a table ?

robert

nathans
Aged Yak Warrior

938 Posts

Posted - 2006-11-01 : 16:38:56
look up
sp_help
in BOL to select constraint info on the table. Also, review the differences between truncate and delete commands.


Nathan Skerl
Go to Top of Page

eurob
Posting Yak Master

100 Posts

Posted - 2006-11-01 : 16:49:54
I know how to pull the constraints from one table, sp_help indeed. I was just wondering if there is something that can loop through all the tables in a database, since I have about 200 tables.
I need to do the truncate because I want to completely remove the contents and have it reset to the original seed value.
If it doesn't work I can do DBCC CHECKIDENT(table,reseed,value).. but I was hoping to just do truncate


robert
Go to Top of Page

nathans
Aged Yak Warrior

938 Posts

Posted - 2006-11-01 : 17:04:00
then why not just script the ddl of the database out and recreate all objects without data?

seems like a lot of overhead to loop through all tables, dynamically create the ALTER statement to drop the constraints, then truncate all tables, then recreate the constraints somehow... sounds like all you want is an empty skeleton.


Nathan Skerl
Go to Top of Page

eurob
Posting Yak Master

100 Posts

Posted - 2006-11-01 : 17:12:41
That's an option. It's rather intense though, I have to reset all the permissions and there are a lot of tables that have definitions in it that I don't want to remove. I just want to truncate a couple of tables that have user generated data in it. What I try to do is execute a DTS package that empties the tables that I selected and repopulate them with data from a test db. It moves all the data fine, however, it comes with 'cannot truncate table' message. Then my seed is off and I have gaps when adding new records. DBCC still works and solves the seed thing, I was just hoping to do this operation appropriately.

robert
Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2006-11-01 : 17:17:20
Take a look at this script. It will show you all the FK references for all tables in a database.

Find Table Reference Levels
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=72957

CODO ERGO SUM
Go to Top of Page

nathans
Aged Yak Warrior

938 Posts

Posted - 2006-11-01 : 17:26:08
for reference: http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=62505

Nathan Skerl
Go to Top of Page

eurob
Posting Yak Master

100 Posts

Posted - 2006-11-01 : 17:30:01
That script is exactly what I was looking for...soo handy! Thanks all for the input.

robert
Go to Top of Page
   

- Advertisement -