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

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

cdn.s3techtraining.com
from cdn.s3techtraining.com More from this publisher
17.06.2013 Views

Chapter 13: User-Defined Functions Summary What we added in this chapter was, in many ways, not new at all. Indeed, much of what goes into userdefined functions is the same set of statements, variables, and general coding practices that we have already seen in scripting and stored procedures. However, UDFs still provide us a wonderful new area of functionality that was not previously available in SQL Server. We can now encapsulate a wider range of code, and even use this encapsulated functionality inline with our queries. What’s more, we can now also provide parameterized views and dynamically created tables. User-defined functions are, in many ways, the most exciting of all the new functionality added to SQL Server. In pondering their uses, I have already come to realize that I’m only scratching the surface of their potential. Over the life of this next release, I suspect that developers will implement UDFs in ways I have yet to dream of — let’s hope you’ll be one of those developers! Exercise 1. Reimplement the spTriangular function from Chapter 12 as a function instead of a stored procedure. 426

14 Transactions and Locks This is one of those chapters that, when you go back to work, makes you sound like you’ve had your Wheaties today. Nothing we’re going to cover in this chapter is wildly difficult, yet transactions and locks tend to be two of the most misunderstood areas in the database world. As such, this beginning (or at least I think it’s a basic) concept is going to make you start to look like a real pro. In this chapter, we’re going to: ❑ Demystify transactions ❑ Examine how the SQL Server log and checkpoints work ❑ Unlock your understanding of locks We’ll learn why these topics are so closely tied to each other and how to minimize problems with each. Transactions Transactions are all about atomicity. Atomicity is the concept that something should act as a unit. From our database standpoint, it’s about the smallest grouping of one or more statements that should be considered to be all or nothing. Often, when dealing with data, we want to make sure that if one thing happens, another thing happens, or that neither of them does. Indeed, this can be carried out to the degree where 20 things (or more) all have to happen together or nothing happens. Let’s look at a classic example. Imagine that you are a banker. Sally comes in and wants to transfer $1,000 from checking to savings. You are, of course, happy to oblige, so you process her request.

14<br />

Transactions and Locks<br />

This is one of those chapters that, when you go back to work, makes you sound like you’ve had<br />

your Wheaties today. Nothing we’re going to cover in this chapter is wildly difficult, yet transactions<br />

and locks tend to be two of the most misunderstood areas in the database world. As such,<br />

this beginning (or at least I think it’s a basic) concept is going to make you start to look like a<br />

real pro.<br />

In this chapter, we’re going to:<br />

❑ Demystify transactions<br />

❑ Examine how the <strong>SQL</strong> <strong>Server</strong> log and checkpoints work<br />

❑ Unlock your understanding of locks<br />

We’ll learn why these topics are so closely tied to each other and how to minimize problems<br />

with each.<br />

Transactions<br />

Transactions are all about atomicity. Atomicity is the concept that something should act as a unit.<br />

From our database standpoint, it’s about the smallest grouping of one or more statements that<br />

should be considered to be all or nothing.<br />

Often, when dealing with data, we want to make sure that if one thing happens, another thing<br />

happens, or that neither of them does. Indeed, this can be carried out to the degree where 20 things<br />

(or more) all have to happen together or nothing happens. Let’s look at a classic example.<br />

Imagine that you are a banker. Sally comes in and wants to transfer $1,000 from checking to savings.<br />

You are, of course, happy to oblige, so you process her request.

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

Saved successfully!

Ooh no, something went wrong!