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.

The WAITFOR statement does exactly what it says it does — that is, it waits for whatever you specify as<br />

the argument to occur. You can specify either an explicit time of day for something to happen, or you can<br />

specify an amount of time to wait before doing something.<br />

The DELAY Parameter<br />

The DELAY parameter choice specifies an amount of time to wait. You cannot specify a number of days —<br />

just time in hours, minutes, and seconds. The maximum allowed delay is 24 hours. So, for example:<br />

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

would run any code prior to the WAITFOR, then reach the WAITFOR statement, and stop for one hour,<br />

after which execution of the code would continue with whatever the next statement was.<br />

The TIME Parameter<br />

The TIME parameter choice specifies to wait until a specific time of day. Again, we cannot specify any<br />

kind of date — just the time of day using a 24-hour clock. Once more, this gives us a one-day time limit<br />

for the maximum amount of delay. For example:<br />

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

would run any code prior to the WAITFOR, then reach the WAITFOR statement, and stop until 1 AM, after<br />

which execution of the code would continue with whatever the next statement was after the WAITFOR.<br />

TRY/CATCH Blocks<br />

In days of yore (meaning anything before <strong>SQL</strong> <strong>Server</strong> 2005), our error-handling options were pretty limited.<br />

We could check for error conditions, but we had to do so proactively. Indeed, in some cases we could<br />

have errors that would cause us to leave our procedure or script without an opportunity to trap it at all<br />

(this can still happen, but we have options that really cut down the instances where it does). We’re going<br />

to save a more full discussion of error handling for our stored procedures discussion in Chapter 12, but<br />

we’ll touch on the fundamentals of the new TRY/CATCH blocks here.<br />

A TRY/CATCH block in <strong>SQL</strong> <strong>Server</strong> works remarkably similarly to those used in any C-derived language<br />

(C, C++, C#, Delphi, and a host of others). The syntax looks like this:<br />

BEGIN TRY<br />

{ }<br />

END TRY<br />

BEGIN CATCH<br />

{ }<br />

END CATCH [ ; ]<br />

Chapter 11: Writing Scripts and Batches<br />

In short, <strong>SQL</strong> <strong>Server</strong> will “try” to run anything within the BEGIN...END that goes with your TRY block.<br />

If, and only if, you have an error condition that has an error level of 11–19 occurs, then <strong>SQL</strong> <strong>Server</strong> will<br />

361

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

Saved successfully!

Ooh no, something went wrong!