CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Dec 2002
    Location
    Beijing
    Posts
    3

    Talking How to generate a unique INT value in VC++?

    I just want to get a unique int value to assign it to every record in the database as primary key, who knows this kind of API? Any information is appreciated.

  2. #2
    Join Date
    Sep 2002
    Posts
    1,747
    Just make your own function that does it serially through a static int. Something like
    Code:
    int ReturnUniqueInt()
    {
         static int counter = 0;
         counter++;
         return counter;
    }
    */*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/

    "It's hard to believe in something you don't understand." -- the sidhi X-files episode

    galathaea: prankster, fablist, magician, liar

  3. #3
    Join Date
    Dec 2002
    Location
    Beijing
    Posts
    3
    This method can not guarantee that every app-start we can get a unique int, but it show me a good hint. Thanks

  4. #4
    Join Date
    Sep 2002
    Posts
    36
    If the purpose is to generate primary key in a database, you should use the "identity" property of your database. It will generate new unique keys itself at each insert...

  5. #5
    Join Date
    May 2002
    Location
    Russia
    Posts
    1,571
    If you use ORACLE there is SEQUENCES in it.

  6. #6
    Join Date
    Sep 2002
    Posts
    77
    If you want to be database independent there is no other way then creating a counter table in the database:

    create table my_counters (
    tablecode int,
    currentvalue int
    )

    Now insert a record for your table which needs a unique id:

    insert into my_counters value (1, 0)

    Then you can query the database for a unique id:

    begin transaction
    update my_counters set currentvalue=currentvalue+1 where tablecode=1
    select countervalue from my_counters where tablecode=1
    commit transaction

  7. #7
    Join Date
    Dec 2002
    Posts
    104
    This solution not good for multi-thread application
    use CriticalSection to modified it.


    int ReturnUniqueInt()
    {
    static int counter = 0;
    counter++;
    return counter;
    }

  8. #8
    Join Date
    Oct 2000
    Posts
    182
    Hi, and what about - at first read by sql question max of this value from table. Then increment it and use it for create new record.
    Alex.

  9. #9
    Join Date
    Sep 2002
    Posts
    77
    Originally posted by AlexEg
    Hi, and what about - at first read by sql question max of this value from table. Then increment it and use it for create new record.
    That's not possible:
    First - try to formulate the SQL commands. It will be something like
    begin transaction
    select max(...) from ...
    insert into values (..., ...)
    end transaction
    How to put the max value into the insert statement?
    And there is no valid SQL command like
    insert into values (select max(...) from ..., ...)

    Second - you will have the problem that multiple clients can read the same max value before one of the clients can do the insert. A select will not lock the row.

  10. #10
    Join Date
    May 2002
    Location
    Russia
    Posts
    1,571
    2alpha137:
    You very very mistake. The following instruction do this
    Code:
    insert into [table1]([field1]) select max([field2]) from [table2]

  11. #11
    Join Date
    Sep 2002
    Posts
    77
    Originally posted by alexey_1979
    2alpha137:
    You very very mistake. The following instruction do this
    Code:
    insert into [table1]([field1]) select max([field2]) from [table2]
    Oh, really?
    I agree that your statement will run against Microsoft databases (MS SQL Server, Access, ...). It may run against Oracle but I'm not sure. But I would be totally surprised if it would run against Informix or DB2.

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

Featured