Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2009-07-04
4
Medium Priority
?
179 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
[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
  • 2
  • 2
4 Comments
 
LVL 143

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 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

715 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