Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Database & SysAdmin
  3. Database
  4. MS SQL: how to truncate all tables without unlink the keys.

MS SQL: how to truncate all tables without unlink the keys.

Scheduled Pinned Locked Moved Database
databasetutorial
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    Ersan Ercek
    wrote on last edited by
    #1

    Is it possible to truncate all tables without touching the constraints. All the best

    L M 2 Replies Last reply
    0
    • E Ersan Ercek

      Is it possible to truncate all tables without touching the constraints. All the best

      L Offline
      L Offline
      leckey 0
      wrote on last edited by
      #2

      By truncate do you mean delete records, but not the table itself? If there are constraints (for example table CUSTOMERS, which has records in SALES by a customer ID) you would have to delete the records from the child table (in this case SALES) first, then delete from the parent (in this case CUSTOMERS). I have had this issue at work where we want to delete something, so we have to start with the "smallest child" and work our way up. If it's something you plan to do frequently then just write a stored procedure for it so you can run when necessary.

      Back in the blog beatch! http://CraptasticNation.blogspot.com/[^]

      E 1 Reply Last reply
      0
      • E Ersan Ercek

        Is it possible to truncate all tables without touching the constraints. All the best

        M Offline
        M Offline
        Mycroft Holmes
        wrote on last edited by
        #3

        It can't be done simply, any table without an key can be truncated and use the following if there is a key

        DELETE FROM TableName
        DBCC CHECKIDENT (TableName, reseed, 1)

        this will have the same effect as truncate. It will only work if there is no DATA based on the foriegn key, it will not violate the referential integrity of the data. I have a script that will clean out a database but it is hard coded for each application. It truncates all the non key table and then walks the key structure of the database manually. You could write a script/app that walked the FK structure of a database and created a hierarchy tree based on the keys (this is what code generators do)

        Never underestimate the power of human stupidity RAH

        E 1 Reply Last reply
        0
        • L leckey 0

          By truncate do you mean delete records, but not the table itself? If there are constraints (for example table CUSTOMERS, which has records in SALES by a customer ID) you would have to delete the records from the child table (in this case SALES) first, then delete from the parent (in this case CUSTOMERS). I have had this issue at work where we want to delete something, so we have to start with the "smallest child" and work our way up. If it's something you plan to do frequently then just write a stored procedure for it so you can run when necessary.

          Back in the blog beatch! http://CraptasticNation.blogspot.com/[^]

          E Offline
          E Offline
          Ersan Ercek
          wrote on last edited by
          #4

          I want truncate because it resets primary key identity as well. Anyway I had some time to search on web and found a sp that drops the foreign keys, truncates the tables and re-creates them. It is very cleaverly written.

          1 Reply Last reply
          0
          • M Mycroft Holmes

            It can't be done simply, any table without an key can be truncated and use the following if there is a key

            DELETE FROM TableName
            DBCC CHECKIDENT (TableName, reseed, 1)

            this will have the same effect as truncate. It will only work if there is no DATA based on the foriegn key, it will not violate the referential integrity of the data. I have a script that will clean out a database but it is hard coded for each application. It truncates all the non key table and then walks the key structure of the database manually. You could write a script/app that walked the FK structure of a database and created a hierarchy tree based on the keys (this is what code generators do)

            Never underestimate the power of human stupidity RAH

            E Offline
            E Offline
            Ersan Ercek
            wrote on last edited by
            #5

            I needed to reset identities as well so, deletion was not enough for me... But at the end I found a good sp that makes all I need.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups