Results 1 to 8 of 8

Thread: Generating New ID

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2003
    Location
    Jakarta, Indonesia
    Posts
    36

    Generating New ID

    I am new to SQL server.
    In MySQL when inserting a new record, im locking the table first,
    use max(ID) + 1 to get a new ID and then release lock after
    succesfully insert a record.

    How you do this in MS SQL ?
    I try to use subquery like
    VB Code:
    1. Insert Into StockGroup Values((SELECT max(id) FROM stockgroup) +1, "test")

    Sub query not allowed.

    Please help.
    Also how get it work in Store Procudure.

    Thanks.

  2. #2
    Hyperactive Member JMvVliet's Avatar
    Join Date
    May 2001
    Location
    Papendrecht, Netherlands
    Posts
    310
    VB Code:
    1. INSERT INTO StockGroup (id, otherfield, and_so_on) SELECT MAX(id) + 1, "otherfield", "and_so_on" FROM StockGroup

    where 'otherfield' and 'and_so_on' are strings.
    This is the right syntaxis.

  3. #3
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    You could also consider using two queries, one for determining the max(id) and the other for the insertion.


    Another (crude) method is to use SQL Server's own version of autonumber. In the design view of the table, allow for increment and seed of 1.

  4. #4

    Thread Starter
    Member
    Join Date
    Mar 2003
    Location
    Jakarta, Indonesia
    Posts
    36
    Originally posted by mendhak
    You could also consider using two queries, one for determining the max(id) and the other for the insertion.
    I wonder how to prevent other user from getting max(ID) when
    there are still another user did the same.
    As I told before I did lock entire table in mySQL to complete this
    task. Table is locked when retrieving max(ID) and inserting a new
    record.
    Autonumber still the best ?

  5. #5
    Hyperactive Member JMvVliet's Avatar
    Join Date
    May 2001
    Location
    Papendrecht, Netherlands
    Posts
    310
    In my opinion autonumber is the best.

  6. #6
    Lively Member blaff's Avatar
    Join Date
    Nov 2002
    Location
    Germany
    Posts
    69
    Another (crude) method is to use SQL Server's own version of autonumber.
    Why crude??
    Normally this is all you need! And you don't have to worry about multiple users getting the same ID, you don't have to lock the table...
    _____
    blaff

  7. #7

    Thread Starter
    Member
    Join Date
    Mar 2003
    Location
    Jakarta, Indonesia
    Posts
    36
    How do you reset autonumber ?

  8. #8
    Fanatic Member
    Join Date
    Sep 2000
    Location
    Over There
    Posts
    522
    hmm
    create a new table just like the one you had??
    do a search, this question has been asked many times.
    It Never Fails. Everytime I try to make a program idiot proof, the world makes a better idiot.

Posting Permissions

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



Click Here to Expand Forum to Full Width