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.

IF NOT EXISTS<br />

(SELECT ‘True’<br />

FROM sys.databases<br />

WHERE name = ‘AdventureWorksCreate’)<br />

BEGIN<br />

CREATE DATABASE AdventureWorksCreate;<br />

END<br />

ELSE<br />

BEGIN<br />

PRINT ‘Database already exists. Skipping CREATE DATABASE Statement’;<br />

END<br />

GO<br />

The first time you run this, there won’t be any database called AdventureWorksCreate (unless by sheer<br />

coincidence you created something called that before we got to this point), so you’ll get a response that<br />

looks like this:<br />

Command(s) completed successfully.<br />

This was unhelpful in terms of telling you what exactly was done, but at least you know it thinks it did<br />

what you asked.<br />

Now run the script a second time and you’ll see a change:<br />

Database already exists. Skipping CREATE DATABASE Statement<br />

Chapter 7: Adding More to Our Queries<br />

So, without much fanfare or fuss, we’ve added a rather small script that will make things much more<br />

usable for the installers of your product. That may be an end user who bought your off-the-shelf product,<br />

or it may be you — in which case it’s even better that it’s fully scripted.<br />

The long and the short of it is that EXISTS is a very handy keyword indeed. It can make some queries<br />

run much faster, and it can also simplify some queries and scripts.<br />

A word of caution here — this is another one of those places where it’s easy to get trapped in “traditional<br />

thinking.” While EXISTS blows other options away in a large percentage of queries where<br />

EXISTS is a valid construct, that’s not always the case. For example, the query used as a derived table<br />

example can also be written with a couple of EXISTS operators (one for each product), but the derived<br />

table happens to run more than twice as fast. That’s definitely the exception, not the rule — EXISTS<br />

will normally smoke a derived table for performance. Just remember that rules are sometimes made to be<br />

broken.<br />

Mixing Data T ypes: CAST and CONVERT<br />

You’ll see both CAST and CONVERT used frequently. Indeed, we’ve touched briefly on both of these<br />

already in this chapter. Considering how often we’ll use these two functions, this seems like a good time<br />

to look a little closer at what they can do for you.<br />

Both CAST and CONVERT perform data-type conversions for you. In most respects, they both do the same<br />

thing, with the exception that CONVERT also does some date-formatting conversions that CAST doesn’t offer.<br />

203

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

Saved successfully!

Ooh no, something went wrong!