All of the documentation on SQL Server deadlocks talks about the scenario in which operation 1 locks resource A then attempts to access resource B and operation 2 locks reso
I'm surprised no one has mentioned the WITH (UPDLOCK)
locking hint. It's very useful if you have deadlocks involving e.g. two select-insert pairs running in parallel.
In SQL Server, if you issue the selects with WITH (UPDLOCK)
, the second select will wait until the first select is finished. Otherwise they get shared locks, and when they simultaneously try to upgrade to exclusive locks, they deadlock.