Solved

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

Posted on 2009-07-04
4
169 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]
Comment Utility
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
Comment Utility
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
Comment Utility
>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
Comment Utility
yes, thanks.  it worked!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

728 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now