Solved

best way to get next ID in database table

Posted on 2010-09-13
8
1,023 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
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 500 total points
ID: 33664327
0
 
LVL 1

Author Closing Comment

by:thready
ID: 33664420
gracius seignior
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

860 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