<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://www2.sqlblog.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Merrill Aldrich : NoLock</title><link>http://www2.sqlblog.com/blogs/merrill_aldrich/archive/tags/NoLock/default.aspx</link><description>Tags: NoLock</description><dc:language>en</dc:language><generator>CommunityServer 2.1 SP2 (Build: 61129.1)</generator><item><title>Transaction Isolation / Dirty Reads / Deadlocks Demo</title><link>http://www2.sqlblog.com/blogs/merrill_aldrich/archive/2009/07/29/transaction-isolation-dirty-reads-deadlocks-demo.aspx</link><pubDate>Wed, 29 Jul 2009 22:44:00 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:15601</guid><dc:creator>merrillaldrich</dc:creator><slash:comments>4</slash:comments><comments>http://www2.sqlblog.com/blogs/merrill_aldrich/comments/15601.aspx</comments><wfw:commentRss>http://www2.sqlblog.com/blogs/merrill_aldrich/commentrss.aspx?PostID=15601</wfw:commentRss><description>&lt;P&gt;I have just given a talk with my group at work on the basics of transaction isolation, dirty reads (nolock) and deadlocks, just as a way to get eveyone on the same page about the tradeoffs between concurrency on a busy transactional system and accuracy of reports. I essentially repackaged a very good example posted by &lt;A class=headermaintitle id=ctl00___ctl00___ctl00_ctl00_bcr_bth___BlogTitle href="http://sqlblog.com/blogs/alexander_kuznetsov/archive/2009/04/10/selects-under-read-committed-and-repeatable-read-may-return-incorrect-results.aspx"&gt;Alexander Kuznetsov&lt;/A&gt;&amp;nbsp;into a simpler script that I could demo to people who are less T-SQL saavy. I hope that he will forgive my re-use of his scenario - I give him full credit for inventing the example :-). Since I'd put together the scripts, I thought I'd throw them up here in case anyone else wants a quick way to demo this. Others have posted similar demos with different emphasis. This particular demo has the advantage (?) that it is very deadlock prone, by the nature of the schema. Both deadlocks and inaccurate dirty reads are readily reproducible.&lt;/P&gt;
&lt;P&gt;So, in a test database, I created two tables, one Entities (People) and one Accounts. In the Entities table, I inserted 1000 "random" people. In Accounts, two rows for each person: account 1 and account 2, with a column for the balance. Everyone gets $50 in each account. I then created a script that would perform repeated transactions, moving $10 for one random person from one of his/her two accounts to the other account. The gist of the demo is to run several simultaneous connections with that script, to simulate load on the system, then run aggregate queries on the accounts and show scenarios for totals errors and for deadlocks.&lt;/P&gt;
&lt;P&gt;Here's the basic setup script:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;&lt;SPAN style="COLOR:blue;"&gt;SET &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;ANSI_NULLS &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;ON
&lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;GO

&lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;SET &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;QUOTED_IDENTIFIER &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;ON
&lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;GO

&lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;CREATE TABLE &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;dbo.Entities&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;(
   &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;EntityID &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;INT &lt;/SPAN&gt;&lt;SPAN style="COLOR:#434343;"&gt;IDENTITY&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;1000&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;,&lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;1&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;) NOT NULL,
   &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;LastName &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;NVARCHAR&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;100&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;) NOT NULL,
   &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;FirstName &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;NVARCHAR&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;100&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;) NOT NULL,
 &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;CONSTRAINT &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;PK_Entities &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;PRIMARY KEY CLUSTERED &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;(
   &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;EntityID &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;ASC
&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;  )
)
&lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;GO

&lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;CREATE TABLE &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;dbo.Accounts&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;(
   &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;EntityID &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;INT &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;NOT NULL,
   &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;AccountID &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;INT &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;NOT NULL,
   &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;Amount decimal&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;18&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;, &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;2&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;) NOT NULL,
 &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;CONSTRAINT &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;PK_Accounts &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;PRIMARY KEY CLUSTERED &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;(
   &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;EntityID &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;ASC&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;,
   &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;AccountID &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;ASC
&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;  )
)
&lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;GO

&lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;ALTER TABLE &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;dbo.Accounts  &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;WITH CHECK ADD  CONSTRAINT &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;FK_Accounts_Entities &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;FOREIGN KEY&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;EntityID&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;)
&lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;REFERENCES &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;dbo.Entities &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;EntityID&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;)
&lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;GO

&lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;ALTER TABLE &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;dbo.Accounts &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;CHECK CONSTRAINT &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;FK_Accounts_Entities
GO&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;PRE&gt;&lt;CODE&gt;
&lt;SPAN style="COLOR:green;"&gt;-- Make 1000 "People"

&lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;DECLARE &lt;/SPAN&gt;&lt;SPAN style="COLOR:#434343;"&gt;@i &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;INT
SET &lt;/SPAN&gt;&lt;SPAN style="COLOR:#434343;"&gt;@i &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;= &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;1
&lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;WHILE &lt;/SPAN&gt;&lt;SPAN style="COLOR:#434343;"&gt;@i &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;&amp;lt;= &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;1000
&lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;BEGIN
   INSERT INTO &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;dbo.Entities &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;( &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;FirstName&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;, &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;LastName &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;) &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;VALUES &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;( &lt;/SPAN&gt;&lt;SPAN style="COLOR:red;"&gt;''&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;, &lt;/SPAN&gt;&lt;SPAN style="COLOR:red;"&gt;'' &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;)
   &lt;/SPAN&gt;&lt;SPAN style="COLOR:green;"&gt;-- Note: you can instead fill in 1000 random names to add a little realism,
   -- but it's not required
   &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;SET &lt;/SPAN&gt;&lt;SPAN style="COLOR:#434343;"&gt;@i &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;= &lt;/SPAN&gt;&lt;SPAN style="COLOR:#434343;"&gt;@i &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;+ &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;1
&lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;END
&lt;/SPAN&gt;&lt;SPAN style="COLOR:green;"&gt;-- Fund the accounts with $50 each

&lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;INSERT INTO &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;dbo.Accounts &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;( &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;EntityID&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;, &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;AccountID&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;, &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;Amount &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;)
&lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;SELECT &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;Entities.EntityID&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;, &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;1 &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;AS &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;AccountID&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;, &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;50.00 &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;FROM &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;dbo.Entities

&lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;INSERT INTO &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;dbo.Accounts &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;( &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;EntityID&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;, &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;AccountID&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;, &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;Amount &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;)
&lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;SELECT &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;Entities.EntityID&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;, &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;2 &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;AS &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;AccountID&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;, &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;50.00 &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;FROM &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;dbo.Entities

&lt;/SPAN&gt;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;To add a bit of realism,&amp;nbsp;you can associate&amp;nbsp;random names to the&amp;nbsp;Entities table,&amp;nbsp;using &lt;A class="" href="http://sqlblog.com/blogs/merrill_aldrich/archive/2009/07/20/random-people-stat.aspx"&gt;this technique&lt;/A&gt;.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then,&amp;nbsp;we have a script that mimics an app connection performing a lot of transactions. The script just moves money from one account to another, so the total balance in the system should remain the same, as well as the balance for every person, making it easy to repro inaccuracies from dirty reads. In a demo, I typically paste this into four separate query windows and get them all running at the same time. They do deadlock periodically, which is a good discussion point. There's logic to continue running after a deadlock:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;
&lt;SPAN style="COLOR:green;"&gt;-- Repeatedly move money for random people

&lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;DECLARE &lt;/SPAN&gt;&lt;SPAN style="COLOR:#434343;"&gt;@randPerson &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;INT
DECLARE &lt;/SPAN&gt;&lt;SPAN style="COLOR:#434343;"&gt;@randAccount &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;INT
DECLARE &lt;/SPAN&gt;&lt;SPAN style="COLOR:#434343;"&gt;@i &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;INT&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;; &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;SET &lt;/SPAN&gt;&lt;SPAN style="COLOR:#434343;"&gt;@i &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;= &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;0
&lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;WHILE &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;( &lt;/SPAN&gt;&lt;SPAN style="COLOR:#434343;"&gt;@i &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;&amp;lt; &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;100000 &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;)
&lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;BEGIN
   SET &lt;/SPAN&gt;&lt;SPAN style="COLOR:#434343;"&gt;@randPerson &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;= &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;( &lt;/SPAN&gt;&lt;SPAN style="COLOR:magenta;"&gt;CAST&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;( &lt;/SPAN&gt;&lt;SPAN style="COLOR:magenta;"&gt;RAND&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;( &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;checksum&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;( &lt;/SPAN&gt;&lt;SPAN style="COLOR:magenta;"&gt;NEWID&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;() ) ) * &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;1000 &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;AS INT &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;) % &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;1000 &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;) + &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;1000&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;;
   &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;SET &lt;/SPAN&gt;&lt;SPAN style="COLOR:#434343;"&gt;@randAccount &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;= &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;( &lt;/SPAN&gt;&lt;SPAN style="COLOR:magenta;"&gt;CAST &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;( &lt;/SPAN&gt;&lt;SPAN style="COLOR:magenta;"&gt;RAND&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;( &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;checksum&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;( &lt;/SPAN&gt;&lt;SPAN style="COLOR:magenta;"&gt;NEWID&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;() ) ) * &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;1000  &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;AS INT &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;) % &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;2 &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;) + &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;1&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;;
   &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;PRINT &lt;/SPAN&gt;&lt;SPAN style="COLOR:#434343;"&gt;@randPerson&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;;
   &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;PRINT &lt;/SPAN&gt;&lt;SPAN style="COLOR:#434343;"&gt;@randAccount&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;;
   
   &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;BEGIN TRY&lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;
       &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;BEGIN TRAN&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;;
           &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;UPDATE &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;dbo.Accounts &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;SET &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;Amount &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;= &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;Amount &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;- &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;10 
               &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;WHERE &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;EntityID &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;= &lt;/SPAN&gt;&lt;SPAN style="COLOR:#434343;"&gt;@randPerson &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;AND &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;AccountID &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;= &lt;/SPAN&gt;&lt;SPAN style="COLOR:#434343;"&gt;@randAccount&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;;
           &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;WAITFOR &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;delay &lt;/SPAN&gt;&lt;SPAN style="COLOR:red;"&gt;'00:00:00.01'&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;;
           &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;UPDATE &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;dbo.Accounts &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;SET &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;Amount &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;= &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;Amount &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;+ &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;10 
               &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;WHERE &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;EntityID &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;= &lt;/SPAN&gt;&lt;SPAN style="COLOR:#434343;"&gt;@randPerson &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;AND &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;AccountID &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;!= &lt;/SPAN&gt;&lt;SPAN style="COLOR:#434343;"&gt;@randAccount&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;;
       &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;COMMIT&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;;
   &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;END TRY&lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;
   
   &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;BEGIN CATCH&lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;
       &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;PRINT &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;ERROR_MESSAGE&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;();
       &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;ROLLBACK&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;;
       &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;RAISERROR&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR:red;"&gt;'Error!'&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;, &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;11&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;, &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;127&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;);
   &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;END CATCH&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;;
   
   &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;SET &lt;/SPAN&gt;&lt;SPAN style="COLOR:#434343;"&gt;@i &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;= &lt;/SPAN&gt;&lt;SPAN style="COLOR:#434343;"&gt;@i &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;+ &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;1&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;;
   &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;WAITFOR &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;delay &lt;/SPAN&gt;&lt;SPAN style="COLOR:red;"&gt;'00:00:00.01'&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;;
&lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;END&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;;

&lt;/SPAN&gt;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;With that script running (x4), in a fifth separate query you can run the following aggregates one at a time to see or demonstrate&amp;nbsp;what is accurate, what deadlocks, and so on. It helps to fire them several times and see the variation in results:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;
&lt;SPAN style="COLOR:green;"&gt;/* code to demo aggregate errors */

&lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;SELECT &lt;/SPAN&gt;&lt;SPAN style="COLOR:magenta;"&gt;SUM&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;( &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;Amount &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;) &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;FROM &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;dbo.Accounts &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;nolock&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;)

&lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
SELECT &lt;/SPAN&gt;&lt;SPAN style="COLOR:magenta;"&gt;SUM&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;( &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;Amount &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;) &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;FROM &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;dbo.Accounts 

&lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;SET TRANSACTION ISOLATION LEVEL READ COMMITTED
SELECT &lt;/SPAN&gt;&lt;SPAN style="COLOR:magenta;"&gt;SUM&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;( &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;Amount &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;) &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;FROM &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;dbo.Accounts 

&lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;SET TRANSACTION ISOLATION LEVEL REPEATABLE READ
SELECT &lt;/SPAN&gt;&lt;SPAN style="COLOR:magenta;"&gt;SUM&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;( &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;Amount &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;) &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;FROM &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;dbo.Accounts

&lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;SET TRANSACTION ISOLATION LEVEL REPEATABLE READ
SELECT &lt;/SPAN&gt;&lt;SPAN style="COLOR:magenta;"&gt;SUM&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;( &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;Amount &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;) &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;FROM &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;dbo.Accounts &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;tablock&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;)

&lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
SELECT &lt;/SPAN&gt;&lt;SPAN style="COLOR:magenta;"&gt;SUM&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;( &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;Amount &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;) &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;FROM &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;dbo.Accounts 

&lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
SELECT &lt;/SPAN&gt;&lt;SPAN style="COLOR:magenta;"&gt;SUM&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;( &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;Amount &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;) &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;FROM &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;dbo.Accounts &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;tablock&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;)

&lt;/SPAN&gt;&lt;SPAN style="COLOR:green;"&gt;-- alter database TransIsolationDemo set ALLOW_SNAPSHOT_ISOLATION on

&lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;SET TRANSACTION ISOLATION LEVEL &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;SNAPSHOT
&lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;SELECT &lt;/SPAN&gt;&lt;SPAN style="COLOR:magenta;"&gt;SUM&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;( &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;Amount &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;) &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;FROM &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;dbo.Accounts 


&lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;SELECT &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;* &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;FROM &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;dbo.Entities e 
&lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;WHERE &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;SELECT &lt;/SPAN&gt;&lt;SPAN style="COLOR:magenta;"&gt;SUM&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;Amount&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;) &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;FROM &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;dbo.Accounts &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;nolock&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;) &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;WHERE &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;Accounts.EntityID &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;= &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;e.EntityID&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;) != &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;100.00

&lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
SELECT &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;* &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;FROM &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;dbo.Entities e 
&lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;WHERE &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;SELECT &lt;/SPAN&gt;&lt;SPAN style="COLOR:magenta;"&gt;SUM&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;Amount&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;) &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;FROM &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;dbo.Accounts&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;WHERE &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;Accounts.EntityID &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;= &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;e.EntityID&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;) != &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;100.00

&lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;SET TRANSACTION ISOLATION LEVEL READ COMMITTED
SELECT &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;* &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;FROM &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;dbo.Entities e 
&lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;WHERE &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;SELECT &lt;/SPAN&gt;&lt;SPAN style="COLOR:magenta;"&gt;SUM&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;Amount&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;) &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;FROM &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;dbo.Accounts &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;WHERE &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;Accounts.EntityID &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;= &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;e.EntityID&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;) != &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;100.00

&lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;SET TRANSACTION ISOLATION LEVEL REPEATABLE READ
SELECT &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;* &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;FROM &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;dbo.Entities e 
&lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;WHERE &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;SELECT &lt;/SPAN&gt;&lt;SPAN style="COLOR:magenta;"&gt;SUM&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;Amount&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;) &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;FROM &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;dbo.Accounts &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;WHERE &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;Accounts.EntityID &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;= &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;e.EntityID&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;) != &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;100.00

&lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;SET TRANSACTION ISOLATION LEVEL REPEATABLE READ
SELECT &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;* &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;FROM &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;dbo.Entities e 
&lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;WHERE &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;SELECT &lt;/SPAN&gt;&lt;SPAN style="COLOR:magenta;"&gt;SUM&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;Amount&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;) &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;FROM &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;dbo.Accounts &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;tablock&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;) &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;WHERE &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;Accounts.EntityID &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;= &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;e.EntityID&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;) != &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;100.00

&lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
SELECT &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;* &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;FROM &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;dbo.Entities e 
&lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;WHERE &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;SELECT &lt;/SPAN&gt;&lt;SPAN style="COLOR:magenta;"&gt;SUM&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;Amount&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;) &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;FROM &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;dbo.Accounts &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;WHERE &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;Accounts.EntityID &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;= &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;e.EntityID&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;) != &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;100.00

&lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
SELECT &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;* &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;FROM &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;dbo.Entities e 
&lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;WHERE &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;SELECT &lt;/SPAN&gt;&lt;SPAN style="COLOR:magenta;"&gt;SUM&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;Amount&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;)) &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;FROM &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;dbo.Accounts &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;tablock&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;) &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;WHERE &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;Accounts.EntityID &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;= &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;e.EntityID&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;) != &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;100.00

&lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;SET TRANSACTION ISOLATION LEVEL &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;SNAPSHOT
&lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;SELECT &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;* &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;FROM &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;dbo.Entities e 
&lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;WHERE &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;SELECT &lt;/SPAN&gt;&lt;SPAN style="COLOR:magenta;"&gt;SUM&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;Amount&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;) &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;FROM &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;dbo.Accounts &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;WHERE &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;Accounts.EntityID &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;= &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;e.EntityID&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;) != &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;100.00

&lt;/SPAN&gt;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;As many others have explained, the only ones that are 100% accurate are Repeatable Read and higher isolation levels.&amp;nbsp;Read Committed&amp;nbsp;and lower will give variable sums both for the table and for each simulated "person." An exception is if you explicitly use (tablock), which is interesting but probably unsuitable for production :-). The demo also shows the opposite problem, which is the increasing likelihood of blocking and deadlocks as you tighten the isolation level. Advantages of Snapshot are also apparent.&lt;/P&gt;
&lt;P&gt;Lastly, it's a decent example to explain deadlocks, and also how to avoid them. In this particular case (this isn't generalizable to other cases) most deadlocks are prevented by locking the two account rows for a person at the beginning of the transaction, instead of having one lock, then a pause, then the other:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;&lt;SPAN style="COLOR:green;"&gt;-- Repeatedly move money for random people avoiding (most) deadlocks

&lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;DECLARE &lt;/SPAN&gt;&lt;SPAN style="COLOR:#434343;"&gt;@randPerson &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;INT
DECLARE &lt;/SPAN&gt;&lt;SPAN style="COLOR:#434343;"&gt;@randAccount &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;INT
DECLARE &lt;/SPAN&gt;&lt;SPAN style="COLOR:#434343;"&gt;@i &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;INT&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;; &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;SET &lt;/SPAN&gt;&lt;SPAN style="COLOR:#434343;"&gt;@i &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;= &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;0
&lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;WHILE &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;( &lt;/SPAN&gt;&lt;SPAN style="COLOR:#434343;"&gt;@i &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;&amp;lt; &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;100000 &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;)
&lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;BEGIN
   SET &lt;/SPAN&gt;&lt;SPAN style="COLOR:#434343;"&gt;@randPerson &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;= &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;( &lt;/SPAN&gt;&lt;SPAN style="COLOR:magenta;"&gt;CAST&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;( &lt;/SPAN&gt;&lt;SPAN style="COLOR:magenta;"&gt;RAND&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;( &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;checksum&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;( &lt;/SPAN&gt;&lt;SPAN style="COLOR:magenta;"&gt;NEWID&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;() ) ) * &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;1000 &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;AS INT &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;) % &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;1000 &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;) + &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;1000&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;;
   &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;SET &lt;/SPAN&gt;&lt;SPAN style="COLOR:#434343;"&gt;@randAccount &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;= &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;( &lt;/SPAN&gt;&lt;SPAN style="COLOR:magenta;"&gt;CAST &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;( &lt;/SPAN&gt;&lt;SPAN style="COLOR:magenta;"&gt;RAND&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;( &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;checksum&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;( &lt;/SPAN&gt;&lt;SPAN style="COLOR:magenta;"&gt;NEWID&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;() ) ) * &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;1000  &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;AS INT &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;) % &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;2 &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;) + &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;1&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;;
   &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;PRINT &lt;/SPAN&gt;&lt;SPAN style="COLOR:#434343;"&gt;@randPerson&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;;
   &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;PRINT &lt;/SPAN&gt;&lt;SPAN style="COLOR:#434343;"&gt;@randAccount&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;;
   
   &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;BEGIN &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;TRY
       &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;BEGIN TRAN&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;;
       
           &lt;/SPAN&gt;&lt;SPAN style="COLOR:green;"&gt;-- Acquire locks on both account rows at the same time:
           &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;SELECT &lt;/SPAN&gt;&lt;SPAN style="COLOR:#434343;"&gt;@temp &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;= &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;Amount &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;FROM &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;dbo.Accounts &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;WITH &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;rowlock&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;, &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;xlock&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;, &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;HOLDLOCK&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;)
           &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;WHERE &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;entityid &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;= &lt;/SPAN&gt;&lt;SPAN style="COLOR:#434343;"&gt;@randperson&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;;

           &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;UPDATE &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;dbo.Accounts &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;SET &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;Amount &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;= &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;Amount &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;- &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;10 
               &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;WHERE &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;EntityID &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;= &lt;/SPAN&gt;&lt;SPAN style="COLOR:#434343;"&gt;@randperson &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;AND &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;AccountID &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;= &lt;/SPAN&gt;&lt;SPAN style="COLOR:#434343;"&gt;@randAccount&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;;
           &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;WAITFOR &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;delay &lt;/SPAN&gt;&lt;SPAN style="COLOR:red;"&gt;'00:00:00.01'&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;;
           &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;UPDATE &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;dbo.Accounts &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;SET &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;Amount &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;= &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;Amount &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;+ &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;10 
               &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;WHERE &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;EntityID &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;= &lt;/SPAN&gt;&lt;SPAN style="COLOR:#434343;"&gt;@randperson &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;AND &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;AccountID &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;!= &lt;/SPAN&gt;&lt;SPAN style="COLOR:#434343;"&gt;@randAccount&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;;
       &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;COMMIT&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;;
   &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;END &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;TRY
   
   &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;BEGIN &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;CATCH
       &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;PRINT &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;ERROR_MESSAGE&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;();
       &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;ROLLBACK&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;;
       &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;RAISERROR&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR:red;"&gt;'Error!'&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;, &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;11&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;, &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;127&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;);
   &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;END &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;CATCH&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;;
   
   &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;SET &lt;/SPAN&gt;&lt;SPAN style="COLOR:#434343;"&gt;@i &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;= &lt;/SPAN&gt;&lt;SPAN style="COLOR:#434343;"&gt;@i &lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;+ &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;1&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;;
   &lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;WAITFOR &lt;/SPAN&gt;&lt;SPAN style="COLOR:black;"&gt;delay &lt;/SPAN&gt;&lt;SPAN style="COLOR:red;"&gt;'00:00:00.01'&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;;
&lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;END&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;;
&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;That's it. Other good examples and discussions are out there:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://sqlblogcasts.com/blogs/tonyrogerson/archive/2006/11/16/1345.aspx"&gt;http://sqlblogcasts.com/blogs/tonyrogerson/archive/2006/11/16/1345.aspx&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&lt;A href="http://sqlblog.com/blogs/alexander_kuznetsov/archive/2009/04/10/selects-under-read-committed-and-repeatable-read-may-return-incorrect-results.aspx"&gt;http://sqlblog.com/blogs/alexander_kuznetsov/archive/2009/04/10/selects-under-read-committed-and-repeatable-read-may-return-incorrect-results.aspx&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&lt;A href="http://sqlblog.com/blogs/andrew_kelly/archive/2009/04/10/how-dirty-are-your-reads.aspx"&gt;http://sqlblog.com/blogs/andrew_kelly/archive/2009/04/10/how-dirty-are-your-reads.aspx&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.neudesic.com/blogs/phil_scott/archive/2005/12/05/11.aspx"&gt;http://blogs.neudesic.com/blogs/phil_scott/archive/2005/12/05/11.aspx&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/CODE&gt;&lt;PRE&gt;&lt;/PRE&gt;&lt;img src="http://www2.sqlblog.com/aggbug.aspx?PostID=15601" width="1" height="1"&gt;</description><category domain="http://www2.sqlblog.com/blogs/merrill_aldrich/archive/tags/Deadlocks/default.aspx">Deadlocks</category><category domain="http://www2.sqlblog.com/blogs/merrill_aldrich/archive/tags/Locking/default.aspx">Locking</category><category domain="http://www2.sqlblog.com/blogs/merrill_aldrich/archive/tags/NoLock/default.aspx">NoLock</category></item></channel></rss>