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
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Resetting 'Identity Specification' column in Sql Table

Resetting 'Identity Specification' column in Sql Table

Scheduled Pinned Locked Moved Database
questionhelpdatabase
12 Posts 6 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.
  • A AmbiguousName

    hello guys... I have this numeric column whose 'Identity Specification' I have set to Yes . But sometimes problem occurs when I use this technique due to the fact that, when I delete all the records from this table, this column's ID does not reset to 1. Instead, it retains the last ID (the ID before deleting all the records). SO the question is: how can I reset this column to 1, after I delete all the records from this table? thnx for any help.

    E Offline
    E Offline
    Ellen_South_Africa
    wrote on last edited by
    #2

    You need to Reseed the Column using the DBCC command of MS SQL Server. dbcc CHECKIDENT("TableName1",RESEED,0) DBCC CHECKIDENT for Microsoft SQL Server I hope this help with your Problem _____________________________________________________________________ Hey guys. Just to defend my answer from the birds and stones :) . The Reason for me using the Delete/DBCC CHECKIDENT Function is that you can still use Auditing on the tables if needed (The Triggers are still called). With a truncate table call the triggers will not be called Truncate Table Because TRUNCATE TABLE is not logged, it cannot activate a trigger. Cheers

    A 1 Reply Last reply
    0
    • A AmbiguousName

      hello guys... I have this numeric column whose 'Identity Specification' I have set to Yes . But sometimes problem occurs when I use this technique due to the fact that, when I delete all the records from this table, this column's ID does not reset to 1. Instead, it retains the last ID (the ID before deleting all the records). SO the question is: how can I reset this column to 1, after I delete all the records from this table? thnx for any help.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #3

      If there are no foreign keys on the table I guess you can always use: truncate table to kill 2 birds with one stone... :-)

      S M 2 Replies Last reply
      0
      • L Lost User

        If there are no foreign keys on the table I guess you can always use: truncate table to kill 2 birds with one stone... :-)

        S Offline
        S Offline
        SilimSayo
        wrote on last edited by
        #4

        Don't let the animal welfare people hear you talk of "killing two birds with one stone". :laugh:

        L 1 Reply Last reply
        0
        • S SilimSayo

          Don't let the animal welfare people hear you talk of "killing two birds with one stone". :laugh:

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #5

          Yes, not very PC of me, obviously if I wanted two birds killed I wouldn't use something as inelegant as a stone, I'd simply put a contract out on them... :-)

          1 Reply Last reply
          0
          • L Lost User

            If there are no foreign keys on the table I guess you can always use: truncate table to kill 2 birds with one stone... :-)

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

            Nah this is so obvious that Ellen's answer had to be the relevant one surely! Besides I'd use a shotgun and get the whole flock (or at least more than 2).

            Never underestimate the power of human stupidity RAH

            L 1 Reply Last reply
            0
            • M Mycroft Holmes

              Nah this is so obvious that Ellen's answer had to be the relevant one surely! Besides I'd use a shotgun and get the whole flock (or at least more than 2).

              Never underestimate the power of human stupidity RAH

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #7

              Hmm, I guess it all depends where you need to use it. I prefer my code to be able to be used in as many situations as possible, therefore to me issuing DML is _always_ preferable to DDL. Besides which, the obvious solution to deleting all rows in a table is to use truncate, the fact that it resets the seed is an added benefit, why make life more difficult for yourself than you need to ? ;)

              M 1 Reply Last reply
              0
              • L Lost User

                Hmm, I guess it all depends where you need to use it. I prefer my code to be able to be used in as many situations as possible, therefore to me issuing DML is _always_ preferable to DDL. Besides which, the obvious solution to deleting all rows in a table is to use truncate, the fact that it resets the seed is an added benefit, why make life more difficult for yourself than you need to ? ;)

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

                unclejimbob wrote:

                I prefer my code to be able to be used in as many situations as possible

                I don't ever recall actually coding a truncate/reseed into a stored proc, in 20+ years. These live under the snippets/template folder in my IDE!

                Never underestimate the power of human stupidity RAH

                L 1 Reply Last reply
                0
                • M Mycroft Holmes

                  unclejimbob wrote:

                  I prefer my code to be able to be used in as many situations as possible

                  I don't ever recall actually coding a truncate/reseed into a stored proc, in 20+ years. These live under the snippets/template folder in my IDE!

                  Never underestimate the power of human stupidity RAH

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #9

                  "I don't ever recall actually coding a truncate/reseed into a stored proc, in 20+ years." Hey, good for you, I use truncate in sps for several - admittedly specialist - purposes. I too have a large number of tsql patterns/idioms as templates. I can't ever recall anyone suggesting that 2 lines of code (delete/reseed) is somehow better than 1 (truncate) and yes, I've been at this game at least as long as you have. You will note that I never suggested my solution was any better or worse than Ellen's I just looked at it from a slightly different angle, taking into account the full content of the question - an approach you might like to try and emulate rather than turning a useful discussion into a point-scoring exercise with you as the self-anointed games master. At 20+ years experience I shouldn't have to be pointing out to you self-evident things such as the fact that the first/most obvious solution isn't necessarily the most appropriate. Oh yeah the bird thing ? Leave it, it started out as witty repartee and you've come in late in the piece and successfully tortured it to death. ___ Updated/final: A few observations on this thread before I go. I have deliberately not read either the most recent post or my email as I assume it contains the same pointless drivel I've been exposed to up to now. 1) Anyone who includes the clause 'I've never needed to do this in x years' is, of course, demeaning your contribution because they mistakenly equate length of service with quality of work; you rarely see this from published, world-class contributors, they tend to be pretty diffident - you don't need to put up with it 2) Indidivuals who come in after the fact and attempt to rate your contribution based upon their own close-enough-is-good-enough two-second evaluation are, of course, putting you down - don't accept it 3) Argumentative postings which don't add value to the discussion are a sign of insecurity over one's position - don't accept it 4) Zero-value postings which are used to simply boost an already over-inflated points-based rating system are, of course to be accepted for what they are 5) Crass, infantile, witless and off-colour postings unrelated to an original throw-away post are a sign that the person is humour-challenged, accept the fact that these people cannot be helped Read the posts in this thread, there are 2 valid answers, 2 witty (albeit off-topic) observations and a host of pointless, unsubstantiated nitpicking - I leave you to decide which of these posts has actual

                  M J 2 Replies Last reply
                  0
                  • L Lost User

                    "I don't ever recall actually coding a truncate/reseed into a stored proc, in 20+ years." Hey, good for you, I use truncate in sps for several - admittedly specialist - purposes. I too have a large number of tsql patterns/idioms as templates. I can't ever recall anyone suggesting that 2 lines of code (delete/reseed) is somehow better than 1 (truncate) and yes, I've been at this game at least as long as you have. You will note that I never suggested my solution was any better or worse than Ellen's I just looked at it from a slightly different angle, taking into account the full content of the question - an approach you might like to try and emulate rather than turning a useful discussion into a point-scoring exercise with you as the self-anointed games master. At 20+ years experience I shouldn't have to be pointing out to you self-evident things such as the fact that the first/most obvious solution isn't necessarily the most appropriate. Oh yeah the bird thing ? Leave it, it started out as witty repartee and you've come in late in the piece and successfully tortured it to death. ___ Updated/final: A few observations on this thread before I go. I have deliberately not read either the most recent post or my email as I assume it contains the same pointless drivel I've been exposed to up to now. 1) Anyone who includes the clause 'I've never needed to do this in x years' is, of course, demeaning your contribution because they mistakenly equate length of service with quality of work; you rarely see this from published, world-class contributors, they tend to be pretty diffident - you don't need to put up with it 2) Indidivuals who come in after the fact and attempt to rate your contribution based upon their own close-enough-is-good-enough two-second evaluation are, of course, putting you down - don't accept it 3) Argumentative postings which don't add value to the discussion are a sign of insecurity over one's position - don't accept it 4) Zero-value postings which are used to simply boost an already over-inflated points-based rating system are, of course to be accepted for what they are 5) Crass, infantile, witless and off-colour postings unrelated to an original throw-away post are a sign that the person is humour-challenged, accept the fact that these people cannot be helped Read the posts in this thread, there are 2 valid answers, 2 witty (albeit off-topic) observations and a host of pointless, unsubstantiated nitpicking - I leave you to decide which of these posts has actual

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

                    unclejimbob wrote:

                    I use truncate in sps for several - admittedly specialist - purposes.

                    As you say , specialised requirements, thankfully I have never actually had to use them.

                    unclejimbob wrote:

                    Taking into account the full content of the question - an approach you might like to try and emulate

                    What and actually give a balanced and reasonable response - Nah. I actually was not denigrating your response as it was a good one (and got an upvote from me) more to the point that the OP probably was not aware of the limitation of the truncate solution.

                    unclejimbob wrote:

                    Oh yeah the bird thing

                    Never, I say never, have I come in late to an obscure discussion and got the wrong handle on on the discussion while trying to be amusing!

                    Never underestimate the power of human stupidity RAH

                    1 Reply Last reply
                    0
                    • E Ellen_South_Africa

                      You need to Reseed the Column using the DBCC command of MS SQL Server. dbcc CHECKIDENT("TableName1",RESEED,0) DBCC CHECKIDENT for Microsoft SQL Server I hope this help with your Problem _____________________________________________________________________ Hey guys. Just to defend my answer from the birds and stones :) . The Reason for me using the Delete/DBCC CHECKIDENT Function is that you can still use Auditing on the tables if needed (The Triggers are still called). With a truncate table call the triggers will not be called Truncate Table Because TRUNCATE TABLE is not logged, it cannot activate a trigger. Cheers

                      A Offline
                      A Offline
                      AmbiguousName
                      wrote on last edited by
                      #11

                      Thnx for your answer. It perfectly solves my problem :)

                      1 Reply Last reply
                      0
                      • L Lost User

                        "I don't ever recall actually coding a truncate/reseed into a stored proc, in 20+ years." Hey, good for you, I use truncate in sps for several - admittedly specialist - purposes. I too have a large number of tsql patterns/idioms as templates. I can't ever recall anyone suggesting that 2 lines of code (delete/reseed) is somehow better than 1 (truncate) and yes, I've been at this game at least as long as you have. You will note that I never suggested my solution was any better or worse than Ellen's I just looked at it from a slightly different angle, taking into account the full content of the question - an approach you might like to try and emulate rather than turning a useful discussion into a point-scoring exercise with you as the self-anointed games master. At 20+ years experience I shouldn't have to be pointing out to you self-evident things such as the fact that the first/most obvious solution isn't necessarily the most appropriate. Oh yeah the bird thing ? Leave it, it started out as witty repartee and you've come in late in the piece and successfully tortured it to death. ___ Updated/final: A few observations on this thread before I go. I have deliberately not read either the most recent post or my email as I assume it contains the same pointless drivel I've been exposed to up to now. 1) Anyone who includes the clause 'I've never needed to do this in x years' is, of course, demeaning your contribution because they mistakenly equate length of service with quality of work; you rarely see this from published, world-class contributors, they tend to be pretty diffident - you don't need to put up with it 2) Indidivuals who come in after the fact and attempt to rate your contribution based upon their own close-enough-is-good-enough two-second evaluation are, of course, putting you down - don't accept it 3) Argumentative postings which don't add value to the discussion are a sign of insecurity over one's position - don't accept it 4) Zero-value postings which are used to simply boost an already over-inflated points-based rating system are, of course to be accepted for what they are 5) Crass, infantile, witless and off-colour postings unrelated to an original throw-away post are a sign that the person is humour-challenged, accept the fact that these people cannot be helped Read the posts in this thread, there are 2 valid answers, 2 witty (albeit off-topic) observations and a host of pointless, unsubstantiated nitpicking - I leave you to decide which of these posts has actual

                        J Offline
                        J Offline
                        jschell
                        wrote on last edited by
                        #12

                        unclejimbob wrote:

                        I can't ever recall anyone suggesting that 2 lines of code (delete/reseed) is somehow better than 1 (truncate) and yes, I've been at this game at least as long as you have.

                        Hopefully that statement is flippant. There are in fact differences between using delete and truncate, especially in SQL Server, which have nothing to do with this discussion. And those differences are the reasons is should be used with care and why it is seldom needed. Choosing the correct one has nothing to do with the number of lines. And there is very little in programmng that should be based solely on line count. (It isn't even a good developer productivity metric.)

                        unclejimbob wrote:

                        Indidivuals who come in after the fact and attempt to rate your contribution based upon their own close-enough-is-good-enough two-second evaluation are, of course, putting you down - don't accept it

                        This site is set up specifically to allow rating answers. Especially on technical questions. That is what the "Rate this message" is for. And one need not even explain why they give it a good or bad rating. Moreover I think that any technical site that doesn't allow and expect discourse on the correctness of answers is probably worthless.

                        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