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.

we are unable or do not want to commit the transaction, this is the point to which all database activity<br />

will be rolled back. That is, everything beyond this point that is not eventually committed will effectively<br />

be forgotten, as far as the database is concerned.<br />

The syntax is:<br />

BEGIN TRAN[SACTION] [|]<br />

[ WITH MARK [ ]]<br />

I won’t dwell on the WITH MARK option here, as it is a topic having to do with very advanced point in<br />

time transaction work, which tends to be more administrator oriented and is well outside of the scope of<br />

this book.<br />

COMMIT TRAN<br />

The committing of a transaction is the end of a completed transaction. At the point that you issue the<br />

COMMIT TRAN, the transaction is considered durable. That is, the effect of the transaction is now permanent<br />

and will last even if you have a system failure (as long as you have a backup or the database files haven’t<br />

been physically destroyed). The only way to undo whatever the transaction accomplished is to issue a<br />

new transaction that, functionally speaking, is a reverse of your first transaction.<br />

The syntax for a COMMIT looks pretty similar to a BEGIN:<br />

COMMIT TRAN[SACTION] [|]<br />

ROLLBACK TRAN<br />

Whenever I think of a ROLLBACK, I think of the old movie The Princess Bride. If you’ve ever seen the film<br />

(if you haven’t, I highly recommend it), you’ll know that the character Vizzini (considered a genius in<br />

the film) always says, “If anything goes wrong, go back to the beginning.”<br />

That is some mighty good advice. A ROLLBACK does just what Vizzini suggests — it goes back to the<br />

beginning. In this case, it’s your transaction that goes back to the beginning. Anything that happened<br />

since the associated BEGIN statement is effectively forgotten. The only exception to going back to the<br />

beginning is through the use of what are called save points, which we’ll describe shortly.<br />

The syntax for a ROLLBACK again looks pretty much the same as a BEGIN or COMMIT, with the exception<br />

of allowance for a save point.<br />

ROLLBACK TRAN[SACTION] [||<br />

|]<br />

SAVE TRAN<br />

Chapter 14: Transactions and Locks<br />

To save a transaction is essentially to create something of a bookmark. You establish a name for your<br />

bookmark (you can have more than one). After this bookmark is established, you can reference it in a<br />

rollback. What’s nice about this is that you can roll back to the exact spot in the code that you want to<br />

just by naming a save point to which you want to roll back.<br />

429

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

Saved successfully!

Ooh no, something went wrong!