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.

Now that we have our sproc created, let’s execute it to see what we get:<br />

EXEC spEmployee;<br />

We get exactly what we would have gotten if we had run the SELECT statement that’s embedded in the sproc:<br />

BusinessEntityID NationalIDNumber LoginID<br />

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

1 295847284 adventure-works\ken0 ---<br />

2 245797967 adventure-works\terri0 ---<br />

3 509647174 adventure-works\roberto0 ---<br />

…<br />

288 954276278 adventure-works\rachel0 ---<br />

289 668991357 adventure-works\jae0 ---<br />

290 134219713 adventure-works\ranjit0 ---<br />

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

Note that I’ve trimmed several columns from the right-hand side of the result set (we can only fit so much in<br />

the width of a book!) — you’ll get additional columns for things like JobTitle, BirthDate, and so on).<br />

You’ve just written your first sproc. It was easy, of course, and frankly, for most situations, sproc writing<br />

isn’t nearly as difficult as most database people would like to have you think (job preservation), but<br />

there are lots of possibilities, and we’ve only seen the beginning.<br />

Changing Stored Procedures with AL TER<br />

I’m going to admit something here — I cut and pasted almost all the text you’re about to read in this and<br />

the next section (“Dropping Sprocs”) from the chapter on views. What I’m pointing out by telling you<br />

this is that they work almost identically from the standpoint of what an ALTER statement does.<br />

The main thing to remember when you edit sprocs with T-<strong>SQL</strong> is that you are completely replacing the<br />

existing sproc. The only differences between using the ALTER PROC statement and the CREATE PROC<br />

statement are:<br />

❑ ALTER PROC expects to find an existing sproc, where CREATE doesn’t.<br />

❑ ALTER PROC retains any permissions (also often referred to as rights) that have been established<br />

for the sproc. It keeps the same object ID within system objects and allows the dependencies to<br />

be kept. For example, if procedure A calls procedure B and you drop and recreate procedure B,<br />

you no longer see the dependency between the two. If you use ALTER, it is all still there.<br />

❑ ALTER PROC retains any dependency information on other objects that may call the sproc being<br />

altered.<br />

The latter of these three is the biggie.<br />

Chapter 12: Stored Procedures<br />

If you perform a DROP and then use a CREATE, you have almost the same effect as<br />

using an ALTER PROC statement with one rather big difference — if you DROP and<br />

CREATE, then you will need to entirely reestablish your permissions on who can and<br />

can’t use the sproc.<br />

369

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

Saved successfully!

Ooh no, something went wrong!