Solved

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

Posted on 2011-02-17
5
647 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
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

828 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