<?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>Defensive database programming: eliminating IF statements.</title><link>http://sqlblog.com/blogs/alexander_kuznetsov/archive/2008/11/27/defensive-database-programming-if-statement-vs-where-clause.aspx</link><description>The following pattern is quite common in database programming: IF EXISTS(some query) BEGIN DO SOMETHING; END When such code runs in high concurrency situations, it may not work as expected. I will provide a repro when such logic fails 40% of the time.</description><dc:language>en</dc:language><generator>CommunityServer 2.1 SP2 (Build: 61129.1)</generator><item><title>re: Defensive database programming: eliminating IF statements.</title><link>http://sqlblog.com/blogs/alexander_kuznetsov/archive/2008/11/27/defensive-database-programming-if-statement-vs-where-clause.aspx#10160</link><pubDate>Fri, 28 Nov 2008 06:43:11 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:10160</guid><dc:creator>Bernd Eckenfels</dc:creator><description>&lt;p&gt;I use this pattern oft for optimistic locking, and I always wonder what to do if rowcount is different from 0 or 1. Sure it should not happen, and shure I can log an alert if it happens. But what does my program logic do? Fail or Proceed. The first one might be a problem for availability, the second one for consitency... but I think I am worrying hypothetically only :)&lt;/p&gt;
</description></item><item><title>re: Defensive database programming: eliminating IF statements.</title><link>http://sqlblog.com/blogs/alexander_kuznetsov/archive/2008/11/27/defensive-database-programming-if-statement-vs-where-clause.aspx#10167</link><pubDate>Fri, 28 Nov 2008 14:21:21 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:10167</guid><dc:creator>Alexander Kuznetsov</dc:creator><description>&lt;p&gt;Bernd,&lt;/p&gt;
&lt;p&gt;In my example there is a UNIQUE INDEX UNQ_TwoInts_ID ON dbo.TwoInts(ID), so my rowcount can only be 0 o1 1. Can you enforce uniqueness in your situation?&lt;/p&gt;
</description></item><item><title>re: Defensive database programming: eliminating IF statements.</title><link>http://sqlblog.com/blogs/alexander_kuznetsov/archive/2008/11/27/defensive-database-programming-if-statement-vs-where-clause.aspx#10188</link><pubDate>Sun, 30 Nov 2008 01:09:30 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:10188</guid><dc:creator>Bernd Eckenfels</dc:creator><description>&lt;p&gt;Yes it is enforced, thats why I wonder what to do if rowcount &amp;gt; 1 :)&lt;/p&gt;
</description></item><item><title>re: Defensive database programming: eliminating IF statements.</title><link>http://sqlblog.com/blogs/alexander_kuznetsov/archive/2008/11/27/defensive-database-programming-if-statement-vs-where-clause.aspx#10189</link><pubDate>Sun, 30 Nov 2008 02:36:42 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:10189</guid><dc:creator>Alexander Kuznetsov</dc:creator><description>&lt;p&gt;Bernd,&lt;/p&gt;
&lt;p&gt;This sounds interesting. Can you post a repro script?&lt;/p&gt;
</description></item><item><title>re: Defensive database programming: eliminating IF statements.</title><link>http://sqlblog.com/blogs/alexander_kuznetsov/archive/2008/11/27/defensive-database-programming-if-statement-vs-where-clause.aspx#10192</link><pubDate>Sun, 30 Nov 2008 13:55:49 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:10192</guid><dc:creator>Richard Howells</dc:creator><description>&lt;p&gt;I’m not sure I understand this article properly.&lt;/p&gt;
&lt;p&gt;At the point when it claims to stop losing updates, that’s not really true. &amp;nbsp;It just keeps trying until an update succeeds. &amp;nbsp;When I added another counter I discovered that to count to 10,000 required about 17,000 attempts. &amp;nbsp;Updates are still being lost just not reported.&lt;/p&gt;
&lt;p&gt;It seems to me that the whole idea is wrong. &amp;nbsp;IMO this is what locking hints are for. &amp;nbsp;I think the SELECT outside the stored procedure should add WITH (UPDLOCK) and that select should be in a transaction that includes the SP call. &amp;nbsp;This allows the outer transaction to be confident that the selected values CAN’T change until the outer transaction commits. &amp;nbsp;Now I can count to 10,000 in 10,000 attempts. &amp;nbsp;This solution is also significantly faster. &amp;nbsp;The article solution takes about 10 seconds on my system. &amp;nbsp;Using the locking hint reduces it to 8 – about 20% faster.&lt;/p&gt;
</description></item><item><title>re: Defensive database programming: eliminating IF statements.</title><link>http://sqlblog.com/blogs/alexander_kuznetsov/archive/2008/11/27/defensive-database-programming-if-statement-vs-where-clause.aspx#10194</link><pubDate>Sun, 30 Nov 2008 15:45:57 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:10194</guid><dc:creator>The Noble Savage</dc:creator><description>&lt;p&gt;Richard - care to share your code with the crew? Your idea sounds interesting.&lt;/p&gt;
</description></item><item><title>re: Defensive database programming: eliminating IF statements.</title><link>http://sqlblog.com/blogs/alexander_kuznetsov/archive/2008/11/27/defensive-database-programming-if-statement-vs-where-clause.aspx#10198</link><pubDate>Sun, 30 Nov 2008 17:05:20 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:10198</guid><dc:creator>Richard Howells</dc:creator><description>&lt;p&gt;@The Noble Savage - Sure. &amp;nbsp;Here's my outer loop, makes no difference which version of the SP is used.&lt;/p&gt;
&lt;p&gt;DECLARE @i1 INT, @i2 INT, @version ROWVERSION, @count INT, @ret INT;&lt;/p&gt;
&lt;p&gt;SET @count = 0;&lt;/p&gt;
&lt;p&gt;WHILE @count&amp;lt;10000 BEGIN&lt;/p&gt;
&lt;p&gt;begin transaction&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;SELECT @i1=i1+1, @i2=i2, @version=version FROM dbo.TwoInts with (updlock) &amp;nbsp;WHERE ID=5;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;EXEC @ret=dbo.UpdateTwoINTs 5, @i1, @i2, @version;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;SET @count = @count + 1 - @ret;&lt;/p&gt;
&lt;p&gt;commit transaction&lt;/p&gt;
&lt;p&gt;END;&lt;/p&gt;
</description></item><item><title>re: Defensive database programming: eliminating IF statements.</title><link>http://sqlblog.com/blogs/alexander_kuznetsov/archive/2008/11/27/defensive-database-programming-if-statement-vs-where-clause.aspx#10199</link><pubDate>Sun, 30 Nov 2008 18:31:06 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:10199</guid><dc:creator>Alexander Kuznetsov</dc:creator><description>&lt;p&gt;Richard,&lt;/p&gt;
&lt;p&gt;To understand my article, you need to know the difference between optimistic and pessimistic locking. Suppose you are making a hotel reservation, you read a row, opened up a screen form, and you are thinking if you want to reserve this room. If the application read the row with (updlock), then other users cannot read this row until you are done with it. This is called pessimistic locking. this is what you are suggesting. It is rarely used in high concurrency OLTP because it usually results in very poor throughput - in real life there could be several minutes between a SELECT and a subsequent EXEC. I am considering a different approach in this article.&lt;/p&gt;
&lt;p&gt;On the other hand, if the application just reads a row without holding locks, then other users cannot read and modify this row while you are making your decision. Then at the time you are saving your changes, you may overwrite other people's changes. This is called a lost update. This is why at the time your changes are being saved, the system needs to detect if the row was modified by somebody else and report an error if it was. This approach is called optimistic locking, that's what I'm saying at the very beginning: &amp;quot;implement optimistic locking using rowversion&amp;quot;, with a strong emphasis on &amp;quot;optimistic&amp;quot;. Each individual update is incrementing either i1 or i2 by one, so when scripts are completed, both i1 and i2 should be 100000 unless some updates were lost.&lt;/p&gt;
&lt;p&gt;HIH&lt;/p&gt;
</description></item><item><title>re: Defensive database programming: eliminating IF statements.</title><link>http://sqlblog.com/blogs/alexander_kuznetsov/archive/2008/11/27/defensive-database-programming-if-statement-vs-where-clause.aspx#10201</link><pubDate>Sun, 30 Nov 2008 20:43:01 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:10201</guid><dc:creator>Chris</dc:creator><description>&lt;p&gt;Or you could just check @@ROWCOUNT. &amp;nbsp;If someone updated to a new version between the if exists and update, the version would be changed an dthe update would fail. &amp;nbsp;Even better, you probably don't need the EXISTS check at all.....&lt;/p&gt;
</description></item><item><title>re: Defensive database programming: eliminating IF statements.</title><link>http://sqlblog.com/blogs/alexander_kuznetsov/archive/2008/11/27/defensive-database-programming-if-statement-vs-where-clause.aspx#10202</link><pubDate>Sun, 30 Nov 2008 20:51:26 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:10202</guid><dc:creator>Alexander Kuznetsov</dc:creator><description>&lt;p&gt;Chris,&lt;/p&gt;
&lt;p&gt;The whole point of this article is precisely this: in most cases you do not need the IF EXISTS check at all.&lt;/p&gt;
</description></item><item><title>re: Defensive database programming: eliminating IF statements.</title><link>http://sqlblog.com/blogs/alexander_kuznetsov/archive/2008/11/27/defensive-database-programming-if-statement-vs-where-clause.aspx#10210</link><pubDate>Mon, 01 Dec 2008 05:07:55 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:10210</guid><dc:creator>Uri Dimant</dc:creator><description>&lt;p&gt;Hi Alex.Great post&lt;/p&gt;
&lt;p&gt;Tony also described the similar technique on his web site.&lt;/p&gt;
&lt;p&gt;&lt;a rel="nofollow" target="_new" href="http://sqlblogcasts.com/blogs/tonyrogerson/archive/2006/06/30/855.aspx"&gt;http://sqlblogcasts.com/blogs/tonyrogerson/archive/2006/06/30/855.aspx&lt;/a&gt;&lt;/p&gt;
</description></item><item><title>re: Defensive database programming: eliminating IF statements.</title><link>http://sqlblog.com/blogs/alexander_kuznetsov/archive/2008/11/27/defensive-database-programming-if-statement-vs-where-clause.aspx#10217</link><pubDate>Mon, 01 Dec 2008 14:32:48 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:10217</guid><dc:creator>Alexander Kuznetsov</dc:creator><description>&lt;p&gt;Hi Uri,&lt;/p&gt;
&lt;p&gt;Yes I like Tony's posts too, I read them all. In this case mutexes are not needed - the single row UPDATE itself under READ COMMITTED works as a mutex.&lt;/p&gt;
</description></item><item><title>re: Defensive database programming: eliminating IF statements.</title><link>http://sqlblog.com/blogs/alexander_kuznetsov/archive/2008/11/27/defensive-database-programming-if-statement-vs-where-clause.aspx#10237</link><pubDate>Tue, 02 Dec 2008 22:47:00 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:10237</guid><dc:creator>Chris Falter</dc:creator><description>&lt;p&gt;In the original SP, many updates got lost because 2 threads both satisfied the IF EXISTS condition and then attempted competing updates. &amp;nbsp;&lt;/p&gt;
&lt;p&gt;Given the new SP, if 2 threads are competing, one of them will fail the WHERE clause, and no update will occur.&lt;/p&gt;
&lt;p&gt;The important thing to note, and I don't think you emphasized it enough, is that in the improved design you *must* check @@ROWCOUNT to make sure the update succeeded, and if not the system *must* take corrective action. &amp;nbsp;IMHO the best way to make sure corrective action is taken is for the improved SP to call RAISERROR if @@ROWCOUNT = 0. &amp;nbsp;I would be reluctant to trust all client code that calls my SP to check the value of @ret and take corrective action; expecting the client code to implement that correctly is an invitation to bug city. &amp;nbsp;Using RAISERROR, though, means that the client code is forced to take corrective action. &amp;nbsp;Sure, your script checks the value of @ret and essentially re-tries if it fails, but I don't know anyone who writes real systems with such simplistic error handling.&lt;/p&gt;
&lt;p&gt;I agree that in the UPDATE case that you have addressed, mutexes are not needed. &amp;nbsp;However, in the insert case, Tony's solution looks very appealing.&lt;/p&gt;
</description></item><item><title>re: Defensive database programming: eliminating IF statements.</title><link>http://sqlblog.com/blogs/alexander_kuznetsov/archive/2008/11/27/defensive-database-programming-if-statement-vs-where-clause.aspx#10247</link><pubDate>Wed, 03 Dec 2008 06:35:59 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:10247</guid><dc:creator>SJ</dc:creator><description>&lt;p&gt;I am novice in database scripting.&lt;/p&gt;
&lt;p&gt;I tried out your example and found that around 45% of the updates were lost.&lt;/p&gt;
&lt;p&gt;But could not fgiure out the reason for the same; please offer insight!&lt;/p&gt;
&lt;p&gt;Thanks!&lt;/p&gt;
</description></item><item><title>re: Defensive database programming: eliminating IF statements.</title><link>http://sqlblog.com/blogs/alexander_kuznetsov/archive/2008/11/27/defensive-database-programming-if-statement-vs-where-clause.aspx#10259</link><pubDate>Wed, 03 Dec 2008 20:14:49 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:10259</guid><dc:creator>Alexander Kuznetsov</dc:creator><description>&lt;p&gt;Chris,&lt;/p&gt;
&lt;p&gt;I am not with you on this &amp;quot;client code is forced to take corrective action&amp;quot; suggestion. Because any database call can raise an exception, it should be wrapped in a try block anyway. A trivial catch block, such as the following:&lt;/p&gt;
&lt;p&gt;catch(Exception e){&lt;/p&gt;
&lt;p&gt;Console.Write(e);&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;will catch an error without any corrective action whatsoever. I don't think that you can force the client to do the right thing by just throwing exceptions. More to the point, instead of Tester-Doer Pattern I used a perfectly legitimate TryParse Pattern, both described here:&lt;/p&gt;
&lt;p&gt;&lt;a rel="nofollow" target="_new" href="http://blogs.msdn.com/kcwalina/archive/2005/03/16/396787.aspx"&gt;http://blogs.msdn.com/kcwalina/archive/2005/03/16/396787.aspx&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Also I disagree with your distrust of client code. I think most client languages such as C# and C++ are far superior to T-SQL, easier to handle errors properly. &amp;nbsp;&lt;/p&gt;
</description></item><item><title>re: Defensive database programming: eliminating IF statements.</title><link>http://sqlblog.com/blogs/alexander_kuznetsov/archive/2008/11/27/defensive-database-programming-if-statement-vs-where-clause.aspx#10260</link><pubDate>Wed, 03 Dec 2008 20:15:44 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:10260</guid><dc:creator>Alexander Kuznetsov</dc:creator><description>&lt;p&gt;SJ,&lt;/p&gt;
&lt;p&gt;Can you be more specific? Which example did you run? I provided several ones.&lt;/p&gt;
</description></item><item><title>re: Defensive database programming: eliminating IF statements.</title><link>http://sqlblog.com/blogs/alexander_kuznetsov/archive/2008/11/27/defensive-database-programming-if-statement-vs-where-clause.aspx#10278</link><pubDate>Fri, 05 Dec 2008 00:35:05 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:10278</guid><dc:creator>JKG</dc:creator><description>&lt;p&gt;Interesting. &amp;nbsp;I've worked on more than a few high demand over worked databases where locking is an issue. They are usually doing a lot of reads and few writes so it's less of a concern but I have to admit i use the IF EXISTS a lot. &amp;nbsp;&lt;/p&gt;
&lt;p&gt;I mostly use it because i like to collapse &amp;quot;Create&amp;quot; and &amp;quot;Update&amp;quot; sprocs so it looks something like...&lt;/p&gt;
&lt;p&gt;IF EXISTS(SELECT 1 FROM dbo.SomeTable WHERE &amp;lt;Some Conditions&amp;gt;) &lt;/p&gt;
&lt;p&gt;BEGIN&lt;/p&gt;
&lt;p&gt; UPDATE dbo.SomeTable SET .. WHERE SomeTableID = @SomeTableId;&lt;/p&gt;
&lt;p&gt;END ELSE BEGIN&lt;/p&gt;
&lt;p&gt; INSERT INTO dbo.SomeTable ...&lt;/p&gt;
&lt;p&gt;END;&lt;/p&gt;
&lt;p&gt;Any better way to handle this situation short forcing the client code to do it? &amp;nbsp;&lt;/p&gt;
&lt;p&gt;I suppose doing the UPDATE first then checking the row count would work but the code seems ugly.&lt;/p&gt;
</description></item><item><title>re: Defensive database programming: eliminating IF statements.</title><link>http://sqlblog.com/blogs/alexander_kuznetsov/archive/2008/11/27/defensive-database-programming-if-statement-vs-where-clause.aspx#10283</link><pubDate>Fri, 05 Dec 2008 01:42:45 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:10283</guid><dc:creator>Alexander Kuznetsov</dc:creator><description>&lt;p&gt;JKG,&lt;/p&gt;
&lt;p&gt;On 2008 you can use the new MERGE command. Prior to that, it is more involved than I could fit in a short answer. Let me write it up over the weekend as a separate post. One thing is clear - moving the logic to the client will not handle race conditions, you will encounter the same problems.&lt;/p&gt;
</description></item><item><title>Defensive database programming: fun with UPDATE.</title><link>http://sqlblog.com/blogs/alexander_kuznetsov/archive/2008/11/27/defensive-database-programming-if-statement-vs-where-clause.aspx#10364</link><pubDate>Mon, 08 Dec 2008 16:04:34 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:10364</guid><dc:creator>Alexander Kuznetsov</dc:creator><description>&lt;p&gt;It is well known that UPDATE ... FROM command does not detect ambiguities. Also it well known that ANSI&lt;/p&gt;
</description></item><item><title>Stress testing UPSERTs</title><link>http://sqlblog.com/blogs/alexander_kuznetsov/archive/2008/11/27/defensive-database-programming-if-statement-vs-where-clause.aspx#10524</link><pubDate>Mon, 15 Dec 2008 01:37:40 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:10524</guid><dc:creator>Alexander Kuznetsov</dc:creator><description>&lt;p&gt;Suppose that you need to implement the following logic: IF(row exists) Update the row ELSE Insert a new&lt;/p&gt;
</description></item><item><title>Defensive database programming: SET vs. SELECT.</title><link>http://sqlblog.com/blogs/alexander_kuznetsov/archive/2008/11/27/defensive-database-programming-if-statement-vs-where-clause.aspx#11457</link><pubDate>Sun, 25 Jan 2009 22:57:02 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:11457</guid><dc:creator>Alexander Kuznetsov</dc:creator><description>&lt;p&gt;Comparing SET vs. SELECT is a very popular topic, and much of what I have to say has been said before.&lt;/p&gt;
</description></item><item><title>re: Defensive database programming: eliminating IF statements.</title><link>http://sqlblog.com/blogs/alexander_kuznetsov/archive/2008/11/27/defensive-database-programming-if-statement-vs-where-clause.aspx#11816</link><pubDate>Tue, 10 Feb 2009 14:02:44 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:11816</guid><dc:creator>Chris Falter</dc:creator><description>&lt;p&gt;Wow, where do I start? &amp;nbsp;First of all, I didn't say I did not trust clients to catch exceptions. &amp;nbsp;I in fact advocated the throwing of an exception from the stored proc precisely because exception handling is far better design for clients than expecting client-side code to pass around (and eventually evaluate/handle) failure codes. &lt;/p&gt;
&lt;p&gt;Secondly, you did not implement the TryParse pattern correctly. &amp;nbsp;For starters, you would need to name your stored proc something like &amp;quot;TRY_UpdateTwoInts&amp;quot; to make it clear to the client that the client must not expect that standard exception handlers will handle the possible error condition. &amp;nbsp;In addition, when you implement the pattern you must also, according to Cwalina, implement a paired exception-throwing member for clients that do not want to make use of the TryParse pattern.&lt;/p&gt;
&lt;p&gt;Third, I seriously doubt the wisdom of using the TryParse pattern in the vast majority of circumstances. &amp;nbsp;In typical production scenarios, it is extraordinarily rare that a race condition would yield an error on more than a few percent of attempted updates, so the performance advantages of TryParse do not outweigh the associated disadvantage of design complexity. &amp;nbsp;I am addressing the general type of situation that the reader of your blog might actually face in his/her server-side code, not an artificial circumstance of a blogger's sample code which is written with the expressed intention of manifesting a race condition.&lt;/p&gt;
</description></item><item><title>re: Defensive database programming: eliminating IF statements.</title><link>http://sqlblog.com/blogs/alexander_kuznetsov/archive/2008/11/27/defensive-database-programming-if-statement-vs-where-clause.aspx#11824</link><pubDate>Tue, 10 Feb 2009 16:28:52 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:11824</guid><dc:creator>Alexander Kuznetsov</dc:creator><description>&lt;p&gt;Chris,&lt;/p&gt;
&lt;p&gt;I agree that TryUpdateTwoInts is a better name.&lt;/p&gt;
</description></item><item><title>Summarizing previous posts about defensive database programming</title><link>http://sqlblog.com/blogs/alexander_kuznetsov/archive/2008/11/27/defensive-database-programming-if-statement-vs-where-clause.aspx#12486</link><pubDate>Mon, 09 Mar 2009 01:49:19 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:12486</guid><dc:creator>Alexander Kuznetsov</dc:creator><description>&lt;p&gt;I have been posting examples of defensive database programming for some time now. I am by no means done&lt;/p&gt;
</description></item><item><title>Defensive database programming: fun with ROWCOUNT</title><link>http://sqlblog.com/blogs/alexander_kuznetsov/archive/2008/11/27/defensive-database-programming-if-statement-vs-where-clause.aspx#12843</link><pubDate>Sun, 22 Mar 2009 03:05:51 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:12843</guid><dc:creator>Alexander Kuznetsov</dc:creator><description>&lt;p&gt;I have written up two examples when a SET ROWCOUNT command breaks a seemingly working stored procedure&lt;/p&gt;
</description></item><item><title>Defensive database programming: fun with triggers.</title><link>http://sqlblog.com/blogs/alexander_kuznetsov/archive/2008/11/27/defensive-database-programming-if-statement-vs-where-clause.aspx#13962</link><pubDate>Tue, 12 May 2009 01:19:33 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:13962</guid><dc:creator>Alexander Kuznetsov</dc:creator><description>&lt;p&gt;There are three kinds of triggers: those which blow up and those which fail silently ;). Seriously, there&lt;/p&gt;
</description></item><item><title>Defensive database programming: conditions in WHERE clause can evaluate in any order DRAFT ONE</title><link>http://sqlblog.com/blogs/alexander_kuznetsov/archive/2008/11/27/defensive-database-programming-if-statement-vs-where-clause.aspx#15213</link><pubDate>Sun, 12 Jul 2009 03:14:15 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:15213</guid><dc:creator>Alexander Kuznetsov</dc:creator><description>&lt;p&gt;My query used to work, but it blows up after I have added an index? The following query is not safe:&lt;/p&gt;
</description></item><item><title>When you add an index and your query blows up...</title><link>http://sqlblog.com/blogs/alexander_kuznetsov/archive/2008/11/27/defensive-database-programming-if-statement-vs-where-clause.aspx#15307</link><pubDate>Thu, 16 Jul 2009 21:41:16 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:15307</guid><dc:creator>Alexander Kuznetsov</dc:creator><description>&lt;p&gt;You cannot assume that the conditions in your WHERE clause will evaluate in the left-to-write order -&lt;/p&gt;
</description></item><item><title>Alexander Kuznetsov : Defensive database programming: fun with changing column widths.</title><link>http://sqlblog.com/blogs/alexander_kuznetsov/archive/2008/11/27/defensive-database-programming-if-statement-vs-where-clause.aspx#17471</link><pubDate>Fri, 09 Oct 2009 20:44:05 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:17471</guid><dc:creator>Alexander Kuznetsov : Defensive database programming: fun with changing column widths.</dc:creator><description>&lt;p&gt;PingBack from &lt;a rel="nofollow" target="_new" href="http://sqlblog.com/blogs/alexander_kuznetsov/archive/2008/11/19/defensive-database-programming-fun-with-changing-column-widths.aspx"&gt;http://sqlblog.com/blogs/alexander_kuznetsov/archive/2008/11/19/defensive-database-programming-fun-with-changing-column-widths.aspx&lt;/a&gt;&lt;/p&gt;
</description></item></channel></rss>