Go Premium for a chance to win a PS4. Enter to Win

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
?
181 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 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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
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. …
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.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

916 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