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.

Next we have a DECLARE statement to declares a variable. We've seen DECLARE statements used in a few<br />

scripts used earlier in the book, but let's visit them a bit more formally.<br />

Declaring Variables<br />

The DECLARE statement has a pretty simple syntax:<br />

DECLARE @ [= ][,<br />

@ [= ][,<br />

@ [= ]]]<br />

You can declare just one variable at a time, or several. It’s common to see people reuse the DECLARE<br />

statement with each variable they declare, rather than use the comma-separated method. It’s up to you,<br />

but no matter which method you choose, you must initialize the variable (using the "=" syntax) or the<br />

value of your variable will be NULL until you explicitly set it to some other value.<br />

In this case, we’ve declared a local variable called @ident as an integer. I've chosen not to initialize it as<br />

the whole purpose of this variable is to accept a value from another source. <strong>Tech</strong>nically, we could have<br />

gotten away without declaring this variable — instead, we could have chosen to just use @@IDENTITY<br />

directly. @@IDENTITY is a system function. It is always available, and supplies the last identity value that<br />

was assigned in the current connection. As with most system functions, you should make a habit of<br />

explicitly moving the value in @@IDENTITY to a local variable. That way, you’re sure that it won’t get<br />

changed accidentally. There was no danger of that in this case, but, as always, be consistent.<br />

Setting the Value in Your Variables<br />

Chapter 11: Writing Scripts and Batches<br />

I like to move a value I’m taking from a system function into my own variable. That<br />

way I can safely use the value and know that it’s being changed only when I change<br />

it. With the system function itself, you sometimes can’t be certain when it’s going to<br />

change because most system functions are not set by you, but by the system. That<br />

creates a situation where it would be very easy to have the system change a value at<br />

a time you weren’t expecting it, and wind up with the most dreaded of all computer<br />

terms: unpredictable results.<br />

Well, we now know how to declare our variables, but the question that follows is, “How do we change<br />

their values?” There are two ways to set the value in a variable. You can use a SELECT statement or a SET<br />

statement. Functionally, they work almost the same, except that a SELECT statement has the power to<br />

have the source value come from a column within the SELECT statement.<br />

So why have two ways of doing this? Actually, I still don’t know. After publishing four books that ask<br />

this very question, I figured someone would e-mail me and give me a good answer — they didn’t. Suffice<br />

to say that SET is now part of the ANSI/ISO standard, and that’s why it’s been put in there. However, I<br />

can’t find anything wrong with the same functionality in SELECT — even ANSI/ISO seems to think that<br />

it’s OK. I’m sure there’s a purpose in the redundancy, but what it is I can’t tell you. That said, there are<br />

some differences in the way they are used in practice.<br />

327

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

Saved successfully!

Ooh no, something went wrong!