Link to home
Start Free TrialLog in
Avatar of ComfortablyNumb
ComfortablyNumbFlag for United Kingdom of Great Britain and Northern Ireland

asked on

TSQL Delete with Referential Integrity

Hi Experts,

I need to clear out some tables from a SQL 2008 database that are all nicely referenced to one another.

Is there a script that can delete/truncate a table and reseed it regardless of whether it is referenced or not?

Cheers,
Numb
ASKER CERTIFIED SOLUTION
Avatar of JaseemK
JaseemK
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Kyle Abrahams, PMP
That's the point of a reference . . . so that you don't break parent child relationships.

If you wanted to write a script for it just start at the bottom table (eg: the table that references nothing) and work  your way up.

The other thing you can do is drop the foreign keys but I would not recommend doing that, as you will break the relationships and have dirty data.  Regardless of what you do, make a backup of the database first!