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.

Chapter 11: Writing Scripts and Batches<br />

<strong>Beginning</strong> with <strong>SQL</strong> <strong>Server</strong> 2005, the sys.messages output got so lengthy that it’s hard to find what<br />

you’re looking for by just scanning it. My solution is less than elegant but is rather effective — I just<br />

artificially create the error I’m looking for and see what error number it gives me (simple solutions for<br />

simple minds like mine!).<br />

I simply execute the code I want to execute (in this case, the CREATE statement) and handle the error if<br />

there is one — there really isn’t much more to it than that.<br />

We will look at error handling in a far more thorough fashion our next chapter. In the meantime, you can<br />

use TRY/CATCH to give basic error handling to your scripts.<br />

Summary<br />

364<br />

Understanding scripts and batches is the cornerstone to an understanding of programming with <strong>SQL</strong><br />

<strong>Server</strong>. The concepts of scripts and batches lay the foundation for a variety of functions from scripting<br />

complete database builds to programming stored procedures and triggers.<br />

Local variables have scope for only one batch. Even if you have declared the variable within the same<br />

overall script, you will still get an error message if you don’t re-declare it (and start over with assigning<br />

values) before referencing it in a new batch.<br />

There are many system functions available. We provided a listing of some of the most useful system functions,<br />

but there are many more. Try checking out the Books Online or Appendix A at the back of this book<br />

for some of the more obscure ones. System functions do not need to be declared, and are always available.<br />

Some are scoped to the entire server, while others return values specific to the current connection.<br />

You can use batches to create precedence between different parts of your scripts. The first batch starts at<br />

the beginning of the script, and ends at the end of the script or the first GO statement — whichever comes<br />

first. The next batch (if there is another) starts on the line after the first one ends and continues to the end<br />

of the script or the next GO statement — again, whichever comes first. The process continues to the end<br />

of the script. The first batch from the top of the script is executed first; the second is executed second,<br />

and so on. All commands within each batch must pass validation in the query parser, or none of that<br />

batch will be executed; however, any other batches will be parsed separately and will still be executed (if<br />

they pass the parser).<br />

We saw how we can create and execute <strong>SQL</strong> dynamically. This can afford us the opportunity to deal with<br />

scenarios that aren’t always 100 percent predictable or situations where something we need to construct<br />

our statement is actually itself a piece of data.<br />

Finally, we took a look at the control of flow constructs that <strong>SQL</strong> <strong>Server</strong> offers. By mixing these constructs,<br />

we are able to conditionally execute code, create loops, or provide for a form of string substitution.<br />

In the next couple of chapters, we will take the notions of scripting and batches to the next level, and<br />

apply them to stored procedures, user-defined functions, and triggers — the closest things that <strong>SQL</strong><br />

<strong>Server</strong> has to actual programs.

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

Saved successfully!

Ooh no, something went wrong!