Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

best way to get next ID in database table

Posted on 2010-09-13
8
Medium Priority
?
1,034 Views
Last Modified: 2012-05-10
Hi Experts,
I want to save the data of a record to the database (which has many rows).  I will need to do this in a transaction.  I want to get what the next ID should be (without using auto-incrementing rows).  So I want some way of locking the table so that I can do the following:
- Lock the table
- Get the next logical ID I should use
- insert my rows
- unlock the table

Is there a way to do this without using database-specific calls?
I could do something like this:
while(true)  {
   while (lock table does not exist)  {
      create lock table
      query other table for max ID
      insert using MaxID + 1
      delete lock table
      return
   }
   sleep (2000)
}
0
Comment
Question by:thready
  • 4
  • 2
  • 2
8 Comments
 
LVL 3

Expert Comment

by:ghayasurrehman
ID: 33663455
insert into table () value ()
select @@scopeidenty


it will return the current id
0
 
LVL 1

Author Comment

by:thready
ID: 33663489
isn't this database-specific?
0
 
LVL 86

Expert Comment

by:jkr
ID: 33663492
>>Is there a way to do this without using database-specific calls?

If you can guarantee that no other process will be accessing the DB during such an operation - yes. If not, I cannot imagine any way that is not specific to the DB. However, if you can use ODBC (http://en.wikipedia.org/wiki/Open_Database_Connectivity), you can reduce the 'specificness' of that.
0
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
LVL 3

Expert Comment

by:ghayasurrehman
ID: 33663502
yes, this is for MS sql server
0
 
LVL 1

Author Comment

by:thready
ID: 33663539
and wouldn't it depend on knowing how columns were created (like auto-incrementing) ?
0
 
LVL 1

Author Comment

by:thready
ID: 33664258
If I must use database-specific, would you know how to do it with Postgres?
0
 
LVL 86

Accepted Solution

by:
jkr earned 2000 total points
ID: 33664327
0
 
LVL 1

Author Closing Comment

by:thready
ID: 33664420
gracius seignior
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

824 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question