Solved

Is returning the ID of an inserted record (VB.NET/SQL Server 2005) always correct?

Posted on 2011-02-17
5
653 Views
Last Modified: 2012-05-11
Hi all,

I have a piece of code that inserts a record into a database table that has an auto ID field and then returns the ID of the record:

            Dim objDB As New Database

            Dim objTemp As Object
            objTemp = objDB.GetScalar("INSERT INTO tbl_person(personname,persondob) VALUES('James Bond','01-Jan-1960') SELECT @@IDENTITY FROM tbl_person")

            If Not objTemp Is Nothing Then

                Response.Write("ID = " & objTemp.ToString)

            Else

                Response.Write("Error!")

            End If

When the code is executed it returns the correct value.

What I would like to know is, if many users are using the same function, will it always return the correct ID?

Is ASP.NET and/or SQL Server setup to handle this, or is it down to me to surround the SQL statement with a mutex object?

All input is greatly appreciated.

resourcesys.
0
Comment
Question by:resourcesys
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 19

Expert Comment

by:Bhavesh Shah
ID: 34915301
Hi,

if you looking for latest one then use

SELECT IDENT_CURRENT('tbl_person')

If you looking for that specific person then

SELECT ID FROM TBL_PERSON WHERE PersonName = 'James Bond'


 @@IDENTITY will give current identity, so if there are multiple inserting happened same time then you might not get correct one,.


- Bhavesh
0
 
LVL 19

Expert Comment

by:Bardobrave
ID: 34915322
You can risk your data with this type of coding if you don't lock the register inserts until id is returned.

Take a look at this article for an introduction to SQL Server locking

http://www.sqlteam.com/article/introduction-to-locking-in-sql-server
0
 

Accepted Solution

by:
resourcesys earned 0 total points
ID: 34915428
Hi,

Thanks for the replies.

After doing some more searching I have found that the SCOPE_IDENTITY() function will return the ID of the newly inserted record and as it is scope specific, the returned value will always be correct.

resourcesys.
0
 
LVL 2

Expert Comment

by:bapcai
ID: 34931304
What I would like to know is, if many users are using the same function, will it always return the correct ID?
Yes, You should use "try catch" and retrun -1 if SQL statement have error. (the seed of identity from 1).

Is ASP.NET and/or SQL Server setup to handle this, or is it down to me to surround the SQL statement with a mutex object? SQL Server do.
0
 

Author Closing Comment

by:resourcesys
ID: 34949655
Solves problem.
0

Featured Post

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

752 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