|
I have the following table:
And the following stored procedure:
This works as I expect it to, and I can uncomment the waitfor delay line and run it in different query windows and it works as expected when used from multiple places at the same time. Now my question is this: Is there is more efficient/elegant way to do this (In SQL 2000)
(comments are locked)
|
|
Here's a method I have used before. But that could result in a multiple threads trying to execute the insert and resulting in a primary key violation: Thread A runs the update and updates nothing. Thread B runs the update and updates nothing. Thread A insert the word. Thread B tries to insert the word and fails because the word was inserted by thread A. You can replicate this by adding a waitfor delay '00:00:10' before the insert and running it in 2 windows (within 10 seconds)
Feb 23 '10 at 10:08 AM
My Other Me
no - if this is in a stored procedure then there is an implicit transaction
Feb 23 '10 at 10:15 AM
Kev Riley ♦♦
I wasn't trying to give you the whole solution, just different methods :)
Feb 23 '10 at 10:19 AM
Kev Riley ♦♦
never had any issues with it in my high-concurrency environments
Feb 23 '10 at 10:36 AM
Kev Riley ♦♦
(comments are locked)
|
|
Here is one alternative Same problem as the Solution from Kev Riley: Not safe for use from multiple threads.
Feb 23 '10 at 10:11 AM
My Other Me
It's a matter of transaction type and locking.
Feb 23 '10 at 10:38 AM
Brimstedt
Thanks for fixing code, Kev :-)
Feb 23 '10 at 10:41 AM
Brimstedt
(comments are locked)
|
|
Another solution,
(comments are locked)
|
|
My typical method for upsert in the past has been like this, though I've built it to work with multiple rows, not just a parameter.
In this case I might
You'd have to wrap in a transaction, and concurrency could be an issue. If you dropped the changes into a table and wrapped a series of updates/insert together, it ought to handle multiple threads better. Is there a PK on word?
(comments are locked)
|
|
No, but - do bear in mind that the only reason that yours works is because the query optimiser doesn't actually really give a crap about your Think about it, how can you hold a row-level lock on a row that doesn't yet exist? So - tread carefully, because the concurrency on this will suck. An alternative way to do it would be as the other people have posted, but using a
(comments are locked)
|

