<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://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>Alexander Kuznetsov : Defensive programming</title><link>http://sqlblog.com/blogs/alexander_kuznetsov/archive/tags/Defensive+programming/default.aspx</link><description>Tags: Defensive programming</description><dc:language>en</dc:language><generator>CommunityServer 2.1 SP2 (Build: 61129.1)</generator><item><title>The client code that handles timeouts</title><link>http://sqlblog.com/blogs/alexander_kuznetsov/archive/2013/01/10/the-client-code-that-handles-timeouts.aspx</link><pubDate>Thu, 10 Jan 2013 21:05:00 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:47107</guid><dc:creator>Alexander Kuznetsov</dc:creator><slash:comments>8</slash:comments><comments>http://sqlblog.com/blogs/alexander_kuznetsov/comments/47107.aspx</comments><wfw:commentRss>http://sqlblog.com/blogs/alexander_kuznetsov/commentrss.aspx?PostID=47107</wfw:commentRss><description>After timeouts we need to make sure that active transactions, if any, are rolled back. All timeout handling must be done on the client. This post provides the implementation and unit tests. Implementation The following class extends SqlCommand and rolls...(&lt;a href="http://sqlblog.com/blogs/alexander_kuznetsov/archive/2013/01/10/the-client-code-that-handles-timeouts.aspx"&gt;read more&lt;/a&gt;)&lt;img src="http://sqlblog.com/aggbug.aspx?PostID=47107" width="1" height="1"&gt;</description><category domain="http://sqlblog.com/blogs/alexander_kuznetsov/archive/tags/Defensive+programming/default.aspx">Defensive programming</category><category domain="http://sqlblog.com/blogs/alexander_kuznetsov/archive/tags/Error+Handling/default.aspx">Error Handling</category><category domain="http://sqlblog.com/blogs/alexander_kuznetsov/archive/tags/T-SQL/default.aspx">T-SQL</category></item><item><title>Yet another use of OUTER APPLY in defensive programming</title><link>http://sqlblog.com/blogs/alexander_kuznetsov/archive/2012/06/06/yet-another-use-of-outer-apply-in-defensive-programming.aspx</link><pubDate>Wed, 06 Jun 2012 21:58:00 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:43779</guid><dc:creator>Alexander Kuznetsov</dc:creator><slash:comments>2</slash:comments><comments>http://sqlblog.com/blogs/alexander_kuznetsov/comments/43779.aspx</comments><wfw:commentRss>http://sqlblog.com/blogs/alexander_kuznetsov/commentrss.aspx?PostID=43779</wfw:commentRss><description>When a SELECT is used to populate variables from a subquery, it fails to change them if the subquery returns nothing - and that can lead to subtle bugs. We shall use OUTER APPLY to eliminate this problem. Prerequisites All we need is the following mock...(&lt;a href="http://sqlblog.com/blogs/alexander_kuznetsov/archive/2012/06/06/yet-another-use-of-outer-apply-in-defensive-programming.aspx"&gt;read more&lt;/a&gt;)&lt;img src="http://sqlblog.com/aggbug.aspx?PostID=43779" width="1" height="1"&gt;</description><category domain="http://sqlblog.com/blogs/alexander_kuznetsov/archive/tags/Defensive+programming/default.aspx">Defensive programming</category><category domain="http://sqlblog.com/blogs/alexander_kuznetsov/archive/tags/Transact+SQL/default.aspx">Transact SQL</category></item><item><title>"Trusted" Foreign Keys Allow Orphans, Reject Valid Child Rows</title><link>http://sqlblog.com/blogs/alexander_kuznetsov/archive/2011/10/17/trusted-foreign-keys-allow-orphans-reject-valid-child-rows.aspx</link><pubDate>Mon, 17 Oct 2011 17:15:00 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:39107</guid><dc:creator>Alexander Kuznetsov</dc:creator><slash:comments>11</slash:comments><comments>http://sqlblog.com/blogs/alexander_kuznetsov/comments/39107.aspx</comments><wfw:commentRss>http://sqlblog.com/blogs/alexander_kuznetsov/commentrss.aspx?PostID=39107</wfw:commentRss><description>In SQL 2008 R2, MERGE does not implement foreign keys properly. I will show both false negatives (valid rows are rejected) and false positives - orphan rows that are allowed to save. False Negatives The following tables implement a very common type/subtype...(&lt;a href="http://sqlblog.com/blogs/alexander_kuznetsov/archive/2011/10/17/trusted-foreign-keys-allow-orphans-reject-valid-child-rows.aspx"&gt;read more&lt;/a&gt;)&lt;img src="http://sqlblog.com/aggbug.aspx?PostID=39107" width="1" height="1"&gt;</description><category domain="http://sqlblog.com/blogs/alexander_kuznetsov/archive/tags/Data+Integrity/default.aspx">Data Integrity</category><category domain="http://sqlblog.com/blogs/alexander_kuznetsov/archive/tags/Defensive+programming/default.aspx">Defensive programming</category><category domain="http://sqlblog.com/blogs/alexander_kuznetsov/archive/tags/Transact+SQL/default.aspx">Transact SQL</category></item><item><title>Reads involving UDFs under READ_COMMITTED_SNAPSHOT may seem inconsistent.</title><link>http://sqlblog.com/blogs/alexander_kuznetsov/archive/2011/08/02/reads-involving-udfs-under-read-committed-snapshot-may-seem-inconsistent.aspx</link><pubDate>Wed, 03 Aug 2011 01:33:00 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:37448</guid><dc:creator>Alexander Kuznetsov</dc:creator><slash:comments>2</slash:comments><comments>http://sqlblog.com/blogs/alexander_kuznetsov/comments/37448.aspx</comments><wfw:commentRss>http://sqlblog.com/blogs/alexander_kuznetsov/commentrss.aspx?PostID=37448</wfw:commentRss><description>When a select uses scalar or multi-statement UDFs under READ_COMMITTED_SNAPSHOT, we might not get consistent results as of the time our select began - I will provide simple repro scripts. At the time of this writing MSDN clearly states the following:...(&lt;a href="http://sqlblog.com/blogs/alexander_kuznetsov/archive/2011/08/02/reads-involving-udfs-under-read-committed-snapshot-may-seem-inconsistent.aspx"&gt;read more&lt;/a&gt;)&lt;img src="http://sqlblog.com/aggbug.aspx?PostID=37448" width="1" height="1"&gt;</description><category domain="http://sqlblog.com/blogs/alexander_kuznetsov/archive/tags/Database+Programming/default.aspx">Database Programming</category><category domain="http://sqlblog.com/blogs/alexander_kuznetsov/archive/tags/Defensive+programming/default.aspx">Defensive programming</category><category domain="http://sqlblog.com/blogs/alexander_kuznetsov/archive/tags/Transact+SQL/default.aspx">Transact SQL</category></item><item><title>Math with Months Is Not Commutative</title><link>http://sqlblog.com/blogs/alexander_kuznetsov/archive/2010/11/29/math-with-months-is-not-commutative.aspx</link><pubDate>Mon, 29 Nov 2010 22:58:00 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:31138</guid><dc:creator>Alexander Kuznetsov</dc:creator><slash:comments>6</slash:comments><comments>http://sqlblog.com/blogs/alexander_kuznetsov/comments/31138.aspx</comments><wfw:commentRss>http://sqlblog.com/blogs/alexander_kuznetsov/commentrss.aspx?PostID=31138</wfw:commentRss><description>In other words, if we add a month, then subtract a month, we might not get back to the date we started from. For example: SELECT DATEADD ( MONTH , 1 , DATEADD ( MONTH , - 1 , '20100330' )) , DATEADD ( MONTH , - 1 , DATEADD ( MONTH , 1 , '20100330' ))...(&lt;a href="http://sqlblog.com/blogs/alexander_kuznetsov/archive/2010/11/29/math-with-months-is-not-commutative.aspx"&gt;read more&lt;/a&gt;)&lt;img src="http://sqlblog.com/aggbug.aspx?PostID=31138" width="1" height="1"&gt;</description><category domain="http://sqlblog.com/blogs/alexander_kuznetsov/archive/tags/Defensive+programming/default.aspx">Defensive programming</category><category domain="http://sqlblog.com/blogs/alexander_kuznetsov/archive/tags/Transact+SQL/default.aspx">Transact SQL</category></item><item><title>Additional links for my presentation on defensive database programming.</title><link>http://sqlblog.com/blogs/alexander_kuznetsov/archive/2010/09/19/additional-links-for-my-presentation-on-defensive-database-programming.aspx</link><pubDate>Sun, 19 Sep 2010 18:29:00 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:28859</guid><dc:creator>Alexander Kuznetsov</dc:creator><slash:comments>1</slash:comments><comments>http://sqlblog.com/blogs/alexander_kuznetsov/comments/28859.aspx</comments><wfw:commentRss>http://sqlblog.com/blogs/alexander_kuznetsov/commentrss.aspx?PostID=28859</wfw:commentRss><description>Yesterday I delivered a session named "Develop T-SQL defensively" at East Iowa SQL Saturday. Thanks to those who attended, and many thanks to Michelle Ufford, Chris Leonard, Ed Leighton-Dick, and other volunteers, who organized the event. Here are the...(&lt;a href="http://sqlblog.com/blogs/alexander_kuznetsov/archive/2010/09/19/additional-links-for-my-presentation-on-defensive-database-programming.aspx"&gt;read more&lt;/a&gt;)&lt;img src="http://sqlblog.com/aggbug.aspx?PostID=28859" width="1" height="1"&gt;</description><category domain="http://sqlblog.com/blogs/alexander_kuznetsov/archive/tags/Defensive+programming/default.aspx">Defensive programming</category><category domain="http://sqlblog.com/blogs/alexander_kuznetsov/archive/tags/SQL+Saturday/default.aspx">SQL Saturday</category><category domain="http://sqlblog.com/blogs/alexander_kuznetsov/archive/tags/SQL+Server/default.aspx">SQL Server</category></item><item><title>Repro scripts and more info for my presentation on concurrency</title><link>http://sqlblog.com/blogs/alexander_kuznetsov/archive/2010/09/19/repro-scripts-and-more-info-for-my-presentation-on-concurrency.aspx</link><pubDate>Sun, 19 Sep 2010 18:10:00 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:28858</guid><dc:creator>Alexander Kuznetsov</dc:creator><slash:comments>0</slash:comments><comments>http://sqlblog.com/blogs/alexander_kuznetsov/comments/28858.aspx</comments><wfw:commentRss>http://sqlblog.com/blogs/alexander_kuznetsov/commentrss.aspx?PostID=28858</wfw:commentRss><description>Yesterday I delivered a session named "Developing T-SQL to survive concurrency" at East Iowa SQL Saturday. Thanks to everyone who attended, and thanks to Michelle Ufford, Chris Leonard, Ed Leighton-Dick, and other volunteers, who organized the event....(&lt;a href="http://sqlblog.com/blogs/alexander_kuznetsov/archive/2010/09/19/repro-scripts-and-more-info-for-my-presentation-on-concurrency.aspx"&gt;read more&lt;/a&gt;)&lt;img src="http://sqlblog.com/aggbug.aspx?PostID=28858" width="1" height="1"&gt;</description><category domain="http://sqlblog.com/blogs/alexander_kuznetsov/archive/tags/Defensive+programming/default.aspx">Defensive programming</category><category domain="http://sqlblog.com/blogs/alexander_kuznetsov/archive/tags/SQL+Deadlocks/default.aspx">SQL Deadlocks</category><category domain="http://sqlblog.com/blogs/alexander_kuznetsov/archive/tags/SQL+Saturday/default.aspx">SQL Saturday</category><category domain="http://sqlblog.com/blogs/alexander_kuznetsov/archive/tags/SQL+Server/default.aspx">SQL Server</category></item><item><title>Speaking at SQL Saturday #50 in Iowa City, IA, and the recent disturbance in the Force.</title><link>http://sqlblog.com/blogs/alexander_kuznetsov/archive/2010/08/29/speaking-at-sql-saturday-50-in-iowa-city-ia-and-the-recent-disturbance-in-the-force.aspx</link><pubDate>Sun, 29 Aug 2010 15:29:00 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:28398</guid><dc:creator>Alexander Kuznetsov</dc:creator><slash:comments>0</slash:comments><comments>http://sqlblog.com/blogs/alexander_kuznetsov/comments/28398.aspx</comments><wfw:commentRss>http://sqlblog.com/blogs/alexander_kuznetsov/commentrss.aspx?PostID=28398</wfw:commentRss><description>In a few weeks I will be meeting a lot of old friends and new folks at SQL Saturday #50 in Iowa City . I'll do two presentations: on defensive database programming and on coding for high concurrency. I really like this whole SQL Saturday thing. For me...(&lt;a href="http://sqlblog.com/blogs/alexander_kuznetsov/archive/2010/08/29/speaking-at-sql-saturday-50-in-iowa-city-ia-and-the-recent-disturbance-in-the-force.aspx"&gt;read more&lt;/a&gt;)&lt;img src="http://sqlblog.com/aggbug.aspx?PostID=28398" width="1" height="1"&gt;</description><category domain="http://sqlblog.com/blogs/alexander_kuznetsov/archive/tags/Defensive+programming/default.aspx">Defensive programming</category></item><item><title>My book is complete, many thanks to Hugo, Tony, and many others!</title><link>http://sqlblog.com/blogs/alexander_kuznetsov/archive/2010/06/18/my-book-is-complete-many-thanks-to-hugo-tony-and-many-others.aspx</link><pubDate>Fri, 18 Jun 2010 19:22:00 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:26267</guid><dc:creator>Alexander Kuznetsov</dc:creator><slash:comments>12</slash:comments><comments>http://sqlblog.com/blogs/alexander_kuznetsov/comments/26267.aspx</comments><wfw:commentRss>http://sqlblog.com/blogs/alexander_kuznetsov/commentrss.aspx?PostID=26267</wfw:commentRss><description>My book is finished. A couple years ago I googled up "Defensive Database Programming", and came up with nothing. Naturally, I started filling up the void. Although the book is complete, I am planning to continue my research; I would appreciate any other...(&lt;a href="http://sqlblog.com/blogs/alexander_kuznetsov/archive/2010/06/18/my-book-is-complete-many-thanks-to-hugo-tony-and-many-others.aspx"&gt;read more&lt;/a&gt;)&lt;img src="http://sqlblog.com/aggbug.aspx?PostID=26267" width="1" height="1"&gt;</description><category domain="http://sqlblog.com/blogs/alexander_kuznetsov/archive/tags/Defensive+programming/default.aspx">Defensive programming</category><category domain="http://sqlblog.com/blogs/alexander_kuznetsov/archive/tags/Transact+SQL/default.aspx">Transact SQL</category></item><item><title>Yet another example of defensive query optimization</title><link>http://sqlblog.com/blogs/alexander_kuznetsov/archive/2010/02/18/yet-another-example-of-defensive-query-optimiziation.aspx</link><pubDate>Thu, 18 Feb 2010 20:11:00 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:22452</guid><dc:creator>Alexander Kuznetsov</dc:creator><slash:comments>1</slash:comments><comments>http://sqlblog.com/blogs/alexander_kuznetsov/comments/22452.aspx</comments><wfw:commentRss>http://sqlblog.com/blogs/alexander_kuznetsov/commentrss.aspx?PostID=22452</wfw:commentRss><description>My second take at the same problem I blogged about yesterday: To optimize a query, we frequently have to explicitly tell optimizer some information which it does not realize by itself. Short term, this works, but long term we run the risk that what we...(&lt;a href="http://sqlblog.com/blogs/alexander_kuznetsov/archive/2010/02/18/yet-another-example-of-defensive-query-optimiziation.aspx"&gt;read more&lt;/a&gt;)&lt;img src="http://sqlblog.com/aggbug.aspx?PostID=22452" width="1" height="1"&gt;</description><category domain="http://sqlblog.com/blogs/alexander_kuznetsov/archive/tags/Defensive+programming/default.aspx">Defensive programming</category><category domain="http://sqlblog.com/blogs/alexander_kuznetsov/archive/tags/query+performance/default.aspx">query performance</category><category domain="http://sqlblog.com/blogs/alexander_kuznetsov/archive/tags/Transact+SQL/default.aspx">Transact SQL</category></item><item><title>Optimizing a query, then documenting assumptions in a unit test.</title><link>http://sqlblog.com/blogs/alexander_kuznetsov/archive/2010/02/17/optimizing-a-query-then-documenting-assumptions-in-a-unit-test.aspx</link><pubDate>Wed, 17 Feb 2010 22:02:00 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:22408</guid><dc:creator>Alexander Kuznetsov</dc:creator><slash:comments>5</slash:comments><comments>http://sqlblog.com/blogs/alexander_kuznetsov/comments/22408.aspx</comments><wfw:commentRss>http://sqlblog.com/blogs/alexander_kuznetsov/commentrss.aspx?PostID=22408</wfw:commentRss><description>To optimize a query, we frequently have to explicitly tell optimizer some information which it does not realize by itself. Short term, this works, but long term we run the risk that what we are telling to the optimizer may be no longer relevant. This...(&lt;a href="http://sqlblog.com/blogs/alexander_kuznetsov/archive/2010/02/17/optimizing-a-query-then-documenting-assumptions-in-a-unit-test.aspx"&gt;read more&lt;/a&gt;)&lt;img src="http://sqlblog.com/aggbug.aspx?PostID=22408" width="1" height="1"&gt;</description><category domain="http://sqlblog.com/blogs/alexander_kuznetsov/archive/tags/Defensive+programming/default.aspx">Defensive programming</category><category domain="http://sqlblog.com/blogs/alexander_kuznetsov/archive/tags/Transact+SQL/default.aspx">Transact SQL</category></item><item><title>SQL Saturday coming to Chicago on Apr 17!</title><link>http://sqlblog.com/blogs/alexander_kuznetsov/archive/2010/02/11/sql-saturday-coming-to-chicago-on-apr-17.aspx</link><pubDate>Thu, 11 Feb 2010 22:08:00 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:22205</guid><dc:creator>Alexander Kuznetsov</dc:creator><slash:comments>0</slash:comments><comments>http://sqlblog.com/blogs/alexander_kuznetsov/comments/22205.aspx</comments><wfw:commentRss>http://sqlblog.com/blogs/alexander_kuznetsov/commentrss.aspx?PostID=22205</wfw:commentRss><description>The schedule is so interesting that I want to get a clone and be in two sessions at the same time. I think of this event as our local PASS Summit Lite – all the benefits like attending sessions and meeting interesting people, but without having to put...(&lt;a href="http://sqlblog.com/blogs/alexander_kuznetsov/archive/2010/02/11/sql-saturday-coming-to-chicago-on-apr-17.aspx"&gt;read more&lt;/a&gt;)&lt;img src="http://sqlblog.com/aggbug.aspx?PostID=22205" width="1" height="1"&gt;</description><category domain="http://sqlblog.com/blogs/alexander_kuznetsov/archive/tags/Defensive+programming/default.aspx">Defensive programming</category><category domain="http://sqlblog.com/blogs/alexander_kuznetsov/archive/tags/SQL+Saturday/default.aspx">SQL Saturday</category></item><item><title>Don't swap horses in midstream.</title><link>http://sqlblog.com/blogs/alexander_kuznetsov/archive/2010/02/01/don-t-swap-horses-in-midstream.aspx</link><pubDate>Mon, 01 Feb 2010 19:07:00 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:21671</guid><dc:creator>Alexander Kuznetsov</dc:creator><slash:comments>2</slash:comments><comments>http://sqlblog.com/blogs/alexander_kuznetsov/comments/21671.aspx</comments><wfw:commentRss>http://sqlblog.com/blogs/alexander_kuznetsov/commentrss.aspx?PostID=21671</wfw:commentRss><description>We can begin a transaction under snapshot isolation, but we cannot switch to it in the middle of an outstanding transaction. For example, the following procedure looks good and passes a smoke test: CREATE PROCEDURE dbo.SelectCountry @CountrySymbol CHAR...(&lt;a href="http://sqlblog.com/blogs/alexander_kuznetsov/archive/2010/02/01/don-t-swap-horses-in-midstream.aspx"&gt;read more&lt;/a&gt;)&lt;img src="http://sqlblog.com/aggbug.aspx?PostID=21671" width="1" height="1"&gt;</description><category domain="http://sqlblog.com/blogs/alexander_kuznetsov/archive/tags/Defensive+programming/default.aspx">Defensive programming</category><category domain="http://sqlblog.com/blogs/alexander_kuznetsov/archive/tags/snapshot+isolation/default.aspx">snapshot isolation</category><category domain="http://sqlblog.com/blogs/alexander_kuznetsov/archive/tags/Transact+SQL/default.aspx">Transact SQL</category></item><item><title>When acquiring locks in the same order is not possible or not feasible.</title><link>http://sqlblog.com/blogs/alexander_kuznetsov/archive/2010/01/15/when-acquiring-locks-in-the-same-order-is-not-possible-or-not-feasible.aspx</link><pubDate>Fri, 15 Jan 2010 21:18:00 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:21095</guid><dc:creator>Alexander Kuznetsov</dc:creator><slash:comments>6</slash:comments><comments>http://sqlblog.com/blogs/alexander_kuznetsov/comments/21095.aspx</comments><wfw:commentRss>http://sqlblog.com/blogs/alexander_kuznetsov/commentrss.aspx?PostID=21095</wfw:commentRss><description>To avoid deadlocks, one of the most common recommendations is "to acquire locks in the same order" or "access objects in the same order". Clearly this makes perfect sense, but is it always feasible? Is it always possible? I keep encountering cases when...(&lt;a href="http://sqlblog.com/blogs/alexander_kuznetsov/archive/2010/01/15/when-acquiring-locks-in-the-same-order-is-not-possible-or-not-feasible.aspx"&gt;read more&lt;/a&gt;)&lt;img src="http://sqlblog.com/aggbug.aspx?PostID=21095" width="1" height="1"&gt;</description><category domain="http://sqlblog.com/blogs/alexander_kuznetsov/archive/tags/Deadlocks/default.aspx">Deadlocks</category><category domain="http://sqlblog.com/blogs/alexander_kuznetsov/archive/tags/Defensive+programming/default.aspx">Defensive programming</category><category domain="http://sqlblog.com/blogs/alexander_kuznetsov/archive/tags/Transact+SQL/default.aspx">Transact SQL</category></item><item><title>T-SQL Tuesday #002: patterns that do not work as expected.</title><link>http://sqlblog.com/blogs/alexander_kuznetsov/archive/2010/01/12/t-sql-tuesday-002-patterns-that-do-not-work-as-expected.aspx</link><pubDate>Tue, 12 Jan 2010 14:38:00 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:20931</guid><dc:creator>Alexander Kuznetsov</dc:creator><slash:comments>14</slash:comments><comments>http://sqlblog.com/blogs/alexander_kuznetsov/comments/20931.aspx</comments><wfw:commentRss>http://sqlblog.com/blogs/alexander_kuznetsov/commentrss.aspx?PostID=20931</wfw:commentRss><description>Neither UPDATE … IF (@@ROWCOUNT = 0) INSERT nor IF EXISTS(...) UPDATE ELSE INSERT patterns work as expected under high concurrency. Both may fail. Both may fail very frequently. MERGE is the king - it holds up much better.Let us do some stress testing...(&lt;a href="http://sqlblog.com/blogs/alexander_kuznetsov/archive/2010/01/12/t-sql-tuesday-002-patterns-that-do-not-work-as-expected.aspx"&gt;read more&lt;/a&gt;)&lt;img src="http://sqlblog.com/aggbug.aspx?PostID=20931" width="1" height="1"&gt;</description><category domain="http://sqlblog.com/blogs/alexander_kuznetsov/archive/tags/Defensive+programming/default.aspx">Defensive programming</category><category domain="http://sqlblog.com/blogs/alexander_kuznetsov/archive/tags/Stress+Testing/default.aspx">Stress Testing</category><category domain="http://sqlblog.com/blogs/alexander_kuznetsov/archive/tags/Transact+SQL/default.aspx">Transact SQL</category></item></channel></rss>