Browse by Tags
» Defensive programming (RSS)
Showing page 2 of 3 (26 total posts)
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 ...
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 I cannot follow this advice.
If I store an object in one parent table and one or more child ...
UPDATE … IF (@@ROWCOUNT = 0) INSERT
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 and see for ourselves.
Here is the table we shall be ...
Some T-SQL code is written under the assumption that either
a TRY block successfully completes or a CATCH block is invoked. Most likely,
this is the case. However, there is a third, although rare, possibility – the
TRY block may fail, and the CATCH one is bypassed. Let me provide some
examples. I do not intend to provide a comprehensive list ...
Although there are many discussions about which kind of cursor or
loop performs the best, there is no doubt which loops perform the worst – the infinite
ones of course. Whenever you write a loop, you need to make sure that it never
runs infinitely. I will provide two examples, two rather common scenarios that
may potentially cause loops to ...
I have written up two examples when a SET ROWCOUNT command
breaks a seemingly working stored procedure or trigger. Note that currently the
best practice is to use TOP clause instead of SET ROWCOUNT, which is
in SQL Server 2008.
However, even if you never use SET ROWCOUNT yourself, some legacy code can still use it – ...
I have been posting examples of defensive database programming
for some time now. I am by no means done with this topic, there is much more to
it. Yet this time I would like to skip concrete examples and write up a long
Defensive programming is an approach that is designed to ensure
high quality of ...
Comparing SET vs. SELECT is a very popular topic, and much
of what I have to say has been said before. Assigning multiple values via
SELECT performs better, and you don’t have to repeat your code several times,
as described by Tony Rogerson here and here. However, if you have ambiguities,
SET will raise an error, but SELECT will not detect ...
Suppose that you need to implement the following logic:
Update the row
Insert a new row.
If you already are on 2008, you should use MERGE command,
and you don’t need to read this post. Prior to 2008 this logic has to be
implemented using UPDATE and INSERT commands. I will stress test several ...
It is well known that UPDATE ... FROM command does not
detect ambiguities. Also it well known that ANSI standard UPDATE may perform very
poorly and may be difficult to maintain, because it does not adhere to the
fundamental DRY (Don’t Repeat Yourself) principle. I will tweak UPDATE ... FROM
in three different ways so that you can detect or ...