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 />

coming up with the same results. The advantage of this is usually more readable code. It is simply easier<br />

to handle a looping structure (or any structure for that matter) if you have a single point of entry and a<br />

single exit. Using a BREAK violates this notion.<br />

All that being said, sometimes you can actually make things worse by reformatting the code to avoid a<br />

BREAK. In addition, I’ve seen people write much slower code for the sake of not using a BREAK statement<br />

— bad idea.<br />

The CONTINUE statement is something of the complete opposite of a BREAK statement. In short, it tells<br />

the WHILE loop to go back to the beginning. Regardless of where you are in the loop, you immediately<br />

go back to the top and re-evaluate the expression (exiting if the expression is no longer TRUE).<br />

We’ll go ahead and do something of a short example here just to get our feet wet. As I mentioned before,<br />

WHILE loops tend to be rare in non-cursor situations, so forgive me if this example seems lame.<br />

What we’re going to do is create something of a monitoring process using our WHILE loop and a WAIT-<br />

FOR command (we’ll look at the specifics of WAITFOR in our next section). We’re going to be automatically<br />

updating our statistics once per day:<br />

WHILE 1 = 1<br />

BEGIN<br />

WAITFOR TIME ‘01:00’;<br />

EXEC sp_updatestats;<br />

RAISERROR(‘Statistics Updated for Database’, 1, 1) WITH LOG;<br />

END<br />

This would update the statistics for every table in our database every night at 1 AM and write a log<br />

entry of that fact to both the <strong>SQL</strong> <strong>Server</strong> log and the Windows application log. If you want check to see if<br />

this works, leave this running all night and then check your logs in the morning.<br />

Note that an infinite loop like this isn’t the way that you would normally want to schedule a task. If you<br />

want something to run every day, set up a job using the Management Studio. In addition to not keeping<br />

a connection open all the time (which the preceding example would do), you also get the capability to<br />

make follow up actions dependent on the success or failure of your script. Also, you can e-mail or netsend<br />

messages regarding the completion status.<br />

The WAITFOR Statement<br />

360<br />

There are often things that you either don’t want to or simply can’t have happen right this moment, but<br />

you also don’t want to have to hang around waiting for the right time to execute something.<br />

No problem — use the WAITFOR statement and have <strong>SQL</strong> <strong>Server</strong> wait for you. The syntax is incredibly simple:<br />

WAITFOR<br />

DELAY | TIME

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

Saved successfully!

Ooh no, something went wrong!