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 11: Writing Scripts and Batches recognize keywords, but also understand their nature. In addition, you have Intellisense, a step debugger, code templates, the object browser, and more. Scripts are usually treated as a unit. That is, you are normally executing the entire script or nothing at all. They can make use of both system functions and local variables. As an example, let’s look at a script that would insert order records in the Accounting database that we created back in Chapter 5: USE Accounting; DECLARE @Ident int; INSERT INTO Orders (CustomerNo,OrderDate, EmployeeID) VALUES (1, GETDATE(), 1); SELECT @Ident = @@IDENTITY; INSERT INTO OrderDetails (OrderID, PartNo, Description, UnitPrice, Qty) VALUES (@Ident, ‘2R2416’, ‘Cylinder Head’, 1300, 2); SELECT ‘The OrderID of the INSERTed row is ‘ + CONVERT(varchar(8),@Ident); If you didn’t keep your populated version of the Accounting database, a script to re-create it in a state usable for this chapter can be downloaded from the Wrox support Website (wrox.com) or my personal support site at www.professionalsql.com. We have six distinct commands working here, covering a range of different things that we might do in a script. We’re using both system functions and local variables, the USE statement, INSERT statements, and both assignment and regular versions of the SELECT statement. They are all working in unison to accomplish one task — to insert complete orders into the database. The USE Statement 326 The USE statement sets the current database. This affects any place where we are making use of default values for the database portion of our fully qualified object name. In this particular example, we have not indicated what database the tables in our INSERT or SELECT statements are from, but, since we’ve included a USE statement prior to our INSERT and SELECT statements, they will use that database (in this case, Accounting). Without our USE statement, we would be at the mercy of whoever executes the script to make certain that the correct database was current when the script was executed. Don’t take this as meaning that you should always include a USE statement in your script — it depends on what the purpose of the script is. If your intent is to have a general-purpose script, then leaving out the USE statement might actually be helpful. Usually, if you are naming database-specific tables in your script (that is, non-system tables), then you want to use the USE command. I also find it very helpful if the script is meant to modify a specific database — as I’ve said in prior chapters, I can’t tell you how many times I’ve accidentally created a large number of tables in the master database that were intended for a user database.

Next we have a DECLARE statement to declares a variable. We've seen DECLARE statements used in a few scripts used earlier in the book, but let's visit them a bit more formally. Declaring Variables The DECLARE statement has a pretty simple syntax: DECLARE @ [= ][, @ [= ][, @ [= ]]] You can declare just one variable at a time, or several. It’s common to see people reuse the DECLARE statement with each variable they declare, rather than use the comma-separated method. It’s up to you, but no matter which method you choose, you must initialize the variable (using the "=" syntax) or the value of your variable will be NULL until you explicitly set it to some other value. In this case, we’ve declared a local variable called @ident as an integer. I've chosen not to initialize it as the whole purpose of this variable is to accept a value from another source. Technically, we could have gotten away without declaring this variable — instead, we could have chosen to just use @@IDENTITY directly. @@IDENTITY is a system function. It is always available, and supplies the last identity value that was assigned in the current connection. As with most system functions, you should make a habit of explicitly moving the value in @@IDENTITY to a local variable. That way, you’re sure that it won’t get changed accidentally. There was no danger of that in this case, but, as always, be consistent. Setting the Value in Your Variables Chapter 11: Writing Scripts and Batches I like to move a value I’m taking from a system function into my own variable. That way I can safely use the value and know that it’s being changed only when I change it. With the system function itself, you sometimes can’t be certain when it’s going to change because most system functions are not set by you, but by the system. That creates a situation where it would be very easy to have the system change a value at a time you weren’t expecting it, and wind up with the most dreaded of all computer terms: unpredictable results. Well, we now know how to declare our variables, but the question that follows is, “How do we change their values?” There are two ways to set the value in a variable. You can use a SELECT statement or a SET statement. Functionally, they work almost the same, except that a SELECT statement has the power to have the source value come from a column within the SELECT statement. So why have two ways of doing this? Actually, I still don’t know. After publishing four books that ask this very question, I figured someone would e-mail me and give me a good answer — they didn’t. Suffice to say that SET is now part of the ANSI/ISO standard, and that’s why it’s been put in there. However, I can’t find anything wrong with the same functionality in SELECT — even ANSI/ISO seems to think that 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 some differences in the way they are used in practice. 327

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!