Note that there are some explanatory texts on larger screens.

plurals
  1. POConflict between UPDATE and SELECT
    primarykey
    data
    text
    <p>I have a table <code>DB.DATA_FEED</code> that I update using a T/SQL Procedure. Every minute, the procedure below is executed 100 times for different data.</p> <pre><code>ALTER PROCEDURE [DB].[UPDATE_DATA_FEED] @P_MARKET_DATE varchar(max), @P_CURR1 int, @P_CURR2 int, @P_PERIOD float(53), @P_MID float(53) AS BEGIN BEGIN TRY UPDATE DB.DATA_FEED SET MID = @P_MID, MARKET_DATE = convert(datetime,@P_MARKET_DATE, 103) WHERE cast(MARKET_DATE as date) = cast(convert(datetime,@P_MARKET_DATE, 103) as date) AND CURR1 = @P_CURR1 AND CURR2 = @P_CURR2 AND PERIOD = @P_PERIOD IF @@TRANCOUNT &gt; 0 COMMIT WORK END TRY BEGIN CATCH --error code END CATCH END END </code></pre> <p>When Users use the application, then they also read from this table, as per the SQL below. Potentially this select can run thousands of times in one minute. (Questions marks are replaced by parser with appropriate date/numbers)</p> <pre><code>DECLARE @MYDATE AS DATE; SET @MYDATE='?' SELECT * FROM DB.DATA_FEED WHERE MARKET_DATE&gt;=@MYDATE AND MARKET_DATE&lt;DATEADD(D,1,@MYDATE) AND CURR1 = ? AND CURR2 = ? AND PERIOD = ? ORDER BY PERIOD </code></pre> <p>I have sometimes, albeit rarely, got a database lock. </p> <p>Using the the script from <a href="http://sqlserverplanet.com/troubleshooting/blocking-processes-lead-blocker" rel="nofollow">http://sqlserverplanet.com/troubleshooting/blocking-processes-lead-blocker</a> I saw it was SPID=58. I then did DECLARE @SPID INT; SET @SPID = 58; DBCC INPUTBUFFER(@SPID) to find the SQL script which turned out to be my select statement. </p> <p>Is there something wrong with my SQL code? What can I do to prevent such locks happening in the future?</p> <p>Thanks</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload