Solved

best way to get next ID in database table

Posted on 2010-09-13
8
1,022 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Foreword This is an old article.  Instead of using the MySQL extension that was used in the original code examples, please choose one of the currently supported database extensions instead.  More information is available here: MySQLi / PDO (http://…
I have been using r1soft Continuous Data Protection (http://www.r1soft.com/linux-cdp/) for many years now with the mySQL Addon and wanted to share a trick I have used several times. For those of us that don't have the luxury of using all transact…
Steps to create a PostgreSQL RDS instance in the Amazon cloud. We will cover some of the default settings and show how to connect to the instance once it is up and running.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

776 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