title: How to simulate a deadlock in SQL Server in a single process?
body: <p>Our client side code detects deadlocks, waits for an interval, then retries the request up to 5 times.  The retry logic detects the deadlocks based on the error number 1205.</p>
  
  <p>My goal is to test both the deadlock retry logic and deadlock handling inside of various stored procedures.  I can create a deadlock using two different connections.  However, I would like to simulate a deadlock inside of a single stored procedure itself.</p>
  
  <p>A deadlock raises the following error message:</p>
  
  <blockquote>
    <p>Msg 1205, Level 13, State 51, Line 1<br>
    Transaction (Process ID 66) was
    deadlocked on lock resources with another process and has been chosen
    as the deadlock victim. Rerun the transaction.</p>
  </blockquote>
  
  <p>I see this error message is in <a href="http://msdn.microsoft.com/en-us/library/ms187382.aspx" rel="noreferrer"><code>sys.messages</code></a>:</p>
  
  <pre><code>select * from sys.messages where message_id = 1205 and language_id = 1033
  
  message_id language_id severity  is_event_logged   text
  1205       1033        13        0                 Transaction (Process ID %d) was deadlocked on %.*ls resources with another process and has been chosen as the deadlock victim. Rerun the transaction.
  </code></pre>
  
  <p>I can't raise this error using <a href="http://msdn.microsoft.com/en-us/library/ms178592.aspx" rel="noreferrer"><code>RAISERROR</code></a>:</p>
  
  <pre><code>raiserror(1205, 13, 51)
  </code></pre>
  
  <blockquote>
    <p>Msg 2732, Level 16, State 1, Line 1<br>
    Error number 1205 is invalid.
    The number must be from 13000 through 2147483647 and it cannot be 50000.</p>
  </blockquote>
  
  <p>Our deadlock retry logic checks if the error number is 1205.  The deadlock needs to have the same message ID, level, and state as a normal deadlock.</p>
  
  <p>Is there a way to simulate a deadlock (with RAISERROR or any other means) and get the same message number out <em>with just one process</em>?</p>
  
  <p>Our databases are using SQL 2005 compatibility, though our servers vary from 2005 through 2008 R2.</p>