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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

7<br />

Adding More to Our Queries<br />

When I first started writing about <strong>SQL</strong> <strong>Server</strong> a number of years ago, I was faced with the question<br />

of when exactly to introduce more complex queries into the knowledge mix — this book faces that<br />

question all over again. At issue is something of a “chicken or egg” thing — talk about scripting,<br />

variables, and the like first, or get to some things that a beginning user might make use of long<br />

before they do server-side scripting. This time around, the notion of more queries early won out.<br />

Some of the concepts in this chapter are going to challenge you with a new way of thinking. You<br />

already had a taste of this just dealing with joins, but you haven’t had to deal with the kind of depth<br />

that I want to challenge you with in this chapter. Even if you don’t have that much procedural programming<br />

experience, the fact is that your brain has a natural tendency to break complex problems<br />

down into their smaller subparts (sub-procedures, logical steps) as opposed to solving them whole<br />

(the “set,” or <strong>SQL</strong> way).<br />

While <strong>SQL</strong> <strong>Server</strong> <strong>2008</strong> supports procedural language concepts now more than ever, my challenge<br />

to you is to try and see the question as a whole first. Be certain that you can’t get it in a single<br />

query. Even if you can’t think of a way, quite often you can break it up into several small queries<br />

and then combine them one at a time back into a larger query that does it all in one task. Try to see<br />

it as a whole and, if you can’t, then go ahead and break it down, but then combine it into the<br />

whole again to the largest extent that makes sense.<br />

This is really what’s at the heart of my challenge of a new way of thinking — conceptualizing the<br />

question as a whole rather than in steps. When we program in most languages, we usually work<br />

in a linear fashion. With <strong>SQL</strong>, however, you need to think more in terms of set theory. You can<br />

liken this to math class and the notion of A union B, or A intersect B. We need to think less in terms<br />

of steps to resolve the data and more about how the data fits together.<br />

In this chapter, we’re going to be using this concept of data fit to ask what amounts to multiple questions<br />

in just one query. Essentially, we’re going to look at ways of taking what seem like multiple<br />

queries and placing them into something that will execute as a complete unit. In addition, we’ll<br />

also be taking a look at query performance and what you can do to get the most out of queries.

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

Saved successfully!

Ooh no, something went wrong!