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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 11: Writing Scripts and Batches<br />

recognize keywords, but also understand their nature. In addition, you have Intellisense, a step debugger,<br />

code templates, the object browser, and more.<br />

Scripts are usually treated as a unit. That is, you are normally executing the entire script or nothing at all.<br />

They can make use of both system functions and local variables. As an example, let’s look at a script that<br />

would insert order records in the Accounting database that we created back in Chapter 5:<br />

USE Accounting;<br />

DECLARE @Ident int;<br />

INSERT INTO Orders<br />

(CustomerNo,OrderDate, EmployeeID)<br />

VALUES<br />

(1, GETDATE(), 1);<br />

SELECT @Ident = @@IDENTITY;<br />

INSERT INTO OrderDetails<br />

(OrderID, PartNo, Description, UnitPrice, Qty)<br />

VALUES<br />

(@Ident, ‘2R2416’, ‘Cylinder Head’, 1300, 2);<br />

SELECT ‘The OrderID of the INSERTed row is ‘ + CONVERT(varchar(8),@Ident);<br />

If you didn’t keep your populated version of the Accounting database, a script to re-create it in a state<br />

usable for this chapter can be downloaded from the Wrox support Website (wrox.com) or my personal<br />

support site at www.professionalsql.com.<br />

We have six distinct commands working here, covering a range of different things that we might do in a<br />

script. We’re using both system functions and local variables, the USE statement, INSERT statements, and<br />

both assignment and regular versions of the SELECT statement. They are all working in unison to accomplish<br />

one task — to insert complete orders into the database.<br />

The USE Statement<br />

326<br />

The USE statement sets the current database. This affects any place where we are making use of default<br />

values for the database portion of our fully qualified object name. In this particular example, we have<br />

not indicated what database the tables in our INSERT or SELECT statements are from, but, since we’ve<br />

included a USE statement prior to our INSERT and SELECT statements, they will use that database (in<br />

this case, Accounting). Without our USE statement, we would be at the mercy of whoever executes the<br />

script to make certain that the correct database was current when the script was executed.<br />

Don’t take this as meaning that you should always include a USE statement in your<br />

script — it depends on what the purpose of the script is. If your intent is to have a<br />

general-purpose script, then leaving out the USE statement might actually be helpful.<br />

Usually, if you are naming database-specific tables in your script (that is, non-system<br />

tables), then you want to use the USE command. I also find it very helpful if the<br />

script is meant to modify a specific database — as I’ve said in prior chapters, I can’t<br />

tell you how many times I’ve accidentally created a large number of tables in the<br />

master database that were intended for a user database.

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

Saved successfully!

Ooh no, something went wrong!