Solved

How do i insert a sql record and get its key with competing multi threads

Posted on 2009-07-04
4
171 Views
Last Modified: 2013-11-30
Hi,
I have a bunch of sync issues.  But here is the beginning of them and i hope its a simple one.  I have multi-threaded  program trying to insert sql records to a table with an autogenerated key.  When i insert the record, I would like to know the key of that record for later updates.  i noticed there is a merge statement, but it wasn't clear to me if it gurantees a "lock" with its statements.  I am not sure of the syntax, but if i use a merge statement to insert a record, and, (i think), get identiy, could two threads call the same merge statement at the same time and the first in line inserts the record, and before it executes the identity statement, the other inserts its record, and now the first gets the second record's identity since it was inserted before its, (the first thread's), identity statement was performed. ...Or, what is the best way to do what i am trying to do.  Not sure this makes any difference, but i am using SQL Compact Edition.
Thanks,
32Handicap

0
Comment
Question by:32handicap
  • 2
  • 2
4 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24777752
with sql ce, you need to use
SELECT @@IDENTITY in a second query using the same connection.

and yes, ce edition does make a difference, as visibly the SCOPE_IDENTITY is not supported...
0
 

Author Comment

by:32handicap
ID: 24777794
Thanks for your quick reply angellll.  I believe both threads could be connected at the same time.  Could the first thread create a record, and before it could do the second querry, the second thread also creates a record.  and then when the first does the "SELECT @@IDENTITY in a second query " it actually gets the second threads record.  Or maybe that is the point of the @@IDENTITY in that it matches up with the connection.
 Thanks
0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 24777812
>I believe both threads could be connected at the same time.
if both threads have their own connection object, it will work just fine.
0
 

Author Closing Comment

by:32handicap
ID: 31599810
yes, thanks.  it worked!
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

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 article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

832 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