TSQL Delete with Referential Integrity

Posted on 2011-05-12
Last Modified: 2012-05-11
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?

Question by:ComfortablyNumb
    LVL 3

    Accepted Solution

    Hi there. Please refer to the following article for your possible solution:

    LVL 39

    Expert Comment

    by:Kyle Abrahams
    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!

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Join & Write a Comment

    Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
    In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.

    731 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now