I have a stored procedure that is doing a MERGE. It seems given the volume of traffic, that two requests are calling INSERT instead of UPDATE at the same time. One of these requests fails because of a foreign key constraint. If I put my merge statement inside a try catch and try to re-execute the sproc, I’m assuming it will run the UPDATE portion of the MERGE this time and succeed. Thoughts on this, is it considered good/bad practice?
I’m using SQL Server 2008 if that matters.
cheers in advance
You can duplicate it in a try/catch block, but the better solution is probably to address the locking when running concurrently. Prior to the merge statement, the canonical way to achieve something like this is as follows:
So, with MERGE, I’d recommend doing the following:
MERGE dbo.Table WITH (HOLDLOCK)…I also recommend testing concurrency using http://www.datamanipulation.net/sqlquerystress/