title: How to prevent multi threaded application to read this same Sql Server record twice
body: <p>I am working on a system that uses multiple threads to read, process and then update database records. Threads run in parallel and try to pick records by calling Sql Server stored procedure.</p>
  
  <p>They call this stored procedure looking for unprocessed records multiple times per second and sometimes pick this same record up.</p>
  
  <p>I try to prevent this happening this way:</p>
  
  <pre><code>UPDATE  dbo.GameData
  SET     Exported = @Now,
          ExportExpires = @Expire,
          ExportSession = @ExportSession
  OUTPUT  Inserted.ID INTO @ExportedIDs
  WHERE ID IN (   SELECT TOP(@ArraySize) GD.ID 
                  FROM        dbo.GameData GD 
                  WHERE       GD.Exported IS NULL 
                  ORDER BY    GD.ID ASC)
  </code></pre>
  
  <p>The idea here is to set a record as exported first using an UPDATE with OUTPUT (remembering record id), so no other thread can pick it up again. When record is set as exported, then I can do some extra calculations and pass the data to the external system hoping that no other thread will pick this same record again in the mean time. Since the UPDATE that has in mind to secure the record first.</p>
  
  <p>Unfortunately it doesn't seem to be working and the application sometimes pick same record twice anyway.</p>
  
  <p>How to prevent it?</p>
  
  <p>Kind regards
  Mariusz</p>