Restarting numbering when deleting tables

  • Is there some way of resetting identity generated number when deleting all records of the table.

    In my particular case I have a table containing daily invoices, where invoice number is an integer identity field (primary key).

    When the working day finishes I move all invoices to a historic table (the primary key is WorkingDate+InvoiceNumber) and delete the daily table.

    I would like to start every day with the number 1 for the identity field "InvoiceNumber". I could drop an recreate the invoices table, but there must exist any other better solution.

     

    Thanks in advance,

    Felix

     

  • If I understand this right you may be looking for..

    dbcc checkident('tblName', reseed, 1)







    **ASCII stupid question, get a stupid ANSI !!!**

  • Did you bother to open BOL on topic "IDENTITY (property)"?

    _____________
    Code for TallyGenerator

  • I'm sorry if I disturbed You. It was not my intention at all. In any case you replied because you wanted to.

    Thanks anyway

  • Not that I'm clamouring for world peace or anything but why is a basic level of amicability so hard to come by ?!?!







    **ASCII stupid question, get a stupid ANSI !!!**

  • Truncating the table will reset the identity number

  • One should always take comments like this to be instructive, which is what it was meant to be! In any case, BOL should be your first point of call - it may even save you time by getting answers to your questions immediately.

  • And what if it were a simultaneous process - what if one was scouring BOL while waiting for an answer from one of the many experts here...?!?!







    **ASCII stupid question, get a stupid ANSI !!!**

  • Neither identity or max(n)+1 will prevent gaps. They both assume the highest value in the table is also the number of values in the table.

    Instead if you want to "issue" invoice numbers to branches, simulate the old paper process. Write your table with a loop or select and then populate it with the numbers you want to use. When you issue one to a branch (or a block as mentioned by Mr. Celko), mark it as issued to that branch, or just issued, depending on your needs.

  • If you are posting a solution that does not works with MS SQL Server in an MS SQL Server Forum what sense does that makes???

     


    * Noel

  • I may not have been in this field as long as you have but I don't consider myself a newbie

    Your "standard" SQL 92 is not by any means the Gospel either. Can you tell how many vendors run their own extensions? ANSWER: ALL

    When posting a solution that runs somewhere else than what the user requested it is absolutely of no use for the poster regardless of maintainability

     

     


    * Noel

  • I would like to point out that this was orginally supposed to be a flamewar over being nice about suggesting that someone read the directions before asking questions.

    Now this.

    We finally get two for the price of one, and I didn't bring any popcorn.

     

  • well guess what...I brought the popcorn but was too rivetted to have any!!







    **ASCII stupid question, get a stupid ANSI !!!**

  • Write a sp "EndOfDay" or update the current (I believe you use one, to transfer the data into history table) to do the following:

    -After tranfser and delete the rows in the current table

    -Alter the table structure

    -Delete the Invoice ID column

    -Insert the column again....

    ------------
    When you 've got a hammer, everything starts to look like a nail...

Viewing 14 posts - 1 through 13 (of 13 total)

You must be logged in to reply to this topic. Login to reply