Sunday, February 19, 2012

ignoring keys while Truncating tables

Hello everyone.

I'm working with a customers business application which is developed in MSSQL. The system has over 170 tables and there is no documentation by those who created it. Now the problem is that I have to write script that deletes all the data in all the tables but since there are foreign keys defined in the tables I can't delete the data. Ofcourse I can figure it out eventually by testing back and forth in which order I have to delete the data in the tables but since there are over 170 tables that could take a very long time.

Does anyone now how I can solve this?? is there for examaple a way to make SQL server to ignore the foreign key lookup? What can I do?? is there any way which I can see in what order I should delete the data in the tables?

appritiate any help or comments.

Thanks.

\Homan1. script out your deletes and run the script 170 times, eventualy you will clear out all the tables.

2. use Enterprise Manager to generate a diagram of all the tables in the database. This would tell you the exact order you would need to follow to get everything deleted.

3. in Enterprise Manager and Query Analyzer you can look up the dpendencies of any object. In EM right click on any object, select all tasks, dependencies, in QA press "F8" to show the object browser, select and object drill down till you see dependencies.

4. use sp_depends on all 170 tables/view to figure out the parents and or child relationships.|||If you are going to have to reload the database you are going to want to know your foreign keys.|||For reference of cascade delete refer to SQL TEam (http://www.sqlteam.com/item.asp?ItemID=8595) link.

Refer to this Code (http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=641&lngWId=5) and modify to accomplish the task.

HTH|||disable your fk checking before deleting data, and re-enable them after.

No comments:

Post a Comment