Results 1 to 5 of 5
  1. #1
    Join Date
    Jan 2007

    Unanswered: auto identity for each Type


    I am working on an accounting system using VB.NET and sql server 2005 as a database. the application should be used by multiple users.
    i have a the following structure:
    Voucher: ID (primary), Date,TypeID, ReferenceCode, .....
    Type: ID, Code, Name. (the user can add new type anytime!)
    (Ex: PV- payment voucher, JV - Journal Voucher ,....)

    When adding a voucher the user will choose a type, according to this type (for each year) a counter will be increminted.
    for example: PV1, PV2....PV233,... the other type will have its separate counter JV1, JV2 ,...JV4569,..
    I am using the sqlTransaction cause i am doing other operations that should be transactional with the insertion of the Voucher.

    The question is :
    What is the best solution to generate a counter for each type?(With code sample)


  2. #2
    Join Date
    May 2004
    do you really need to have the 'PV' and 'JV' before each value? if you could use ints, then you could use identity columns. That's the standard way of doing this.

    You can always tack on a JV or PV in the front end if that's the way your boss wants it to look in a report or something.

    from BOL:


    Indicates that the new column is an identity column. When a new row is added to the table, Microsoft® SQL Server™ provides a unique, incremental value for the column. Identity columns are commonly used in conjunction with PRIMARY KEY constraints to serve as the unique row identifier for the table. The IDENTITY property can be assigned to tinyint, smallint, int, bigint, decimal(p,0), or numeric(p,0) columns. Only one identity column can be created per table. Bound defaults and DEFAULT constraints cannot be used with an identity column. You must specify both the seed and increment or neither. If neither is specified, the default is (1,1).

  3. #3
    Join Date
    Apr 2002
    Toronto, Canada
    if you were using mysql, this functionality (starting a new auto_increment within each type group) is built in

    it's impossible to do this with an IDENTITY column

    you will have to generate your own numbers, and i would recommend very strongly against it | @rudydotca
    Buy my SitePoint book: Simply SQL

  4. #4
    Join Date
    Jan 2007
    the counter in the question is the ReferenceCode in the Voucher table
    Voucher: ID (primary), Date,TypeID, ReferenceCode.
    so for each added voucher and according to the TypeID a the reference code will be generated. let say the last counter for the PV type is 230 so the referenceCode will be PV231. if the Type is JV and the last counter is 566 then the ReferenceCode will be JV567 and so on.
    We don't have to forget that we are working in a multi user enviroment, and the Reference Code should be unique .

  5. #5
    Join Date
    Nov 2004
    on the wrong server
    Provided Answers: 6
    put the JV or PV in another field and concatenate it in the front end. smart numbers are stupid and loved by the accounting types. this kind of things slow down joins and causes other kinds of pain. i have not seen smart numbers in a project for five years and that was a legacy foxpro app.
    “If one brings so much courage to this world the world has to kill them or break them, so of course it kills them. The world breaks every one and afterward many are strong at the broken places. But those that will not break it kills. It kills the very good and the very gentle and the very brave impartially. If you are none of these you can be sure it will kill you too but there will be no special hurry.” Earnest Hemingway, A Farewell To Arms.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts