17.06.2013 Views

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 14: Transactions and Locks<br />

Behind the scenes, something like this is happening:<br />

UPDATE checking<br />

SET Balance = Balance - 1000<br />

WHERE Account = ‘Sally’<br />

UPDATE savings<br />

SET Balance = Balance + 1000<br />

WHERE Account = ‘Sally’<br />

This is a hyper-simplification of what’s going on, but it captures the main thrust of things: You need to<br />

issue two different statements — one for each account.<br />

Now what if the first statement executes and the second one doesn’t? Sally would be out a thousand dollars!<br />

That might, for a short time, seem OK from your perspective (heck, you just made a thousand<br />

bucks!), but not for long. By that afternoon you’d have a steady stream of customers leaving your<br />

bank — it’s hard to stay in the bank business with no depositors.<br />

What you need is a way to be certain that if the first statement executes, the second statement executes.<br />

There really isn’t a way that we can be certain of that — all sorts of things can go wrong, from hardware<br />

failures to simple things, such as violations of data integrity rules. Fortunately, however, there is a way<br />

to do something that serves the same overall purpose — we can essentially forget that the first statement<br />

ever happened. We can enforce at least the notion that if one thing didn’t happen, then nothing did — at<br />

least within the scope of our transaction.<br />

In order to capture this notion of a transaction, however, we need to be able to define very definite<br />

boundaries. A transaction has to have very definitive beginning and end points. Actually, every SELECT,<br />

INSERT, UPDATE, and DELETE statement you issue in <strong>SQL</strong> <strong>Server</strong> is part of an implicit transaction. Even<br />

if you issue only one statement, that one statement is considered to be a transaction — everything about<br />

the statement will be executed, or none of it will. Indeed, by default, that is the length of a transaction —<br />

one statement.<br />

But what if we need to have more than one statement be all or nothing, as in our preceding bank example?<br />

In such a case, we need a way of marking the beginning and end of a transaction, as well as the success<br />

or failure of that transaction. To that end, there are several T-<strong>SQL</strong> statements that we can use to mark<br />

these points in a transaction. We can:<br />

❑ BEGIN a transaction: Set the starting point.<br />

❑ COMMIT a transaction: Make the transaction a permanent, irreversible part of the database.<br />

❑ ROLLBACK a transaction: Say essentially that we want to forget that it ever happened.<br />

❑ SAVE a transaction: Establish a specific marker to allow us to do only a partial rollback.<br />

Let’s look over all of these individually before we put them together into our first transaction.<br />

BEGIN TRAN<br />

428<br />

The beginning of the transaction is probably one of the easiest concepts to understand in the transaction<br />

process. Its sole purpose in life is to denote the point that is the beginning of a unit. If, for some reason,

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!