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.

ELSE -- hmm, we don’t recognize it, so report it and bail<br />

RAISERROR(‘something not good happened this time around’, 16, 1 );<br />

END CATCH<br />

If you run this in a database that does not already have the OurIFTest table, then you will get a simple:<br />

Command(s) completed successfully.<br />

But run it where the OurIFTest table already exists (for example, run it twice if you haven’t run the<br />

CREATE code before) and you get something to indicate the error:<br />

WARNING: Skipping CREATE as table already exists<br />

A error was logged. The Log ID for our error was 3<br />

Note that the actual error number you see will depend on whether the ErrorLog table has already had<br />

other errors inserted in it before you ran this test.<br />

Now run a little select against the error log table:<br />

SELECT *<br />

FROM ErrorLog<br />

WHERE ErrorLogID = 3; -- change this value to whatever your<br />

-- results said it was logged as<br />

And you can see that the error was indeed properly logged:<br />

ErrorLogID UserName ErrorMessage<br />

----------- ----------- ---------------------------------------------------<br />

3 dbo There is already an object named ‘OurIFTest’ ...<br />

(1 row(s) affected)<br />

Chapter 12: Stored Procedures<br />

There are several things that you should take note of between the sproc itself and the usage of it by the<br />

calling script:<br />

❑ The OUTPUT keyword was required for the output parameter in the sproc declaration.<br />

❑ You must use the OUTPUT keyword when you call the sproc, much as you did when you<br />

declared the sproc. This gives <strong>SQL</strong> <strong>Server</strong> advance warning about the special handling that<br />

parameter will require. Be aware, however, that forgetting to include the OUTPUT keyword won’t<br />

create a runtime error (you won’t get any messages about it), but the value for the output parameter<br />

won’t be moved into your variable (you’ll just wind up with what was already there —<br />

most likely a NULL value). This means that you’ll have what I consider to be the most dreaded<br />

of all computer terms — unpredictable results.<br />

❑ The variable you assign the output result to does not have to have the same name as the internal<br />

parameter in the sproc. For example, in our previous sproc, the internal parameter in the error<br />

logging sproc was called @ErrorLogID, but the variable the value was passed to was called<br />

@MyOutputParameter.<br />

❑ The EXEC (or EXECUTE) keyword was required since the call to the sproc wasn’t the first thing in<br />

the batch (you can leave off the EXEC if the sproc call is the first thing in a batch) — personally, I<br />

recommend that you train yourself to use it regardless.<br />

375

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

Saved successfully!

Ooh no, something went wrong!