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.

Chapter 12: Stored Procedures<br />

Creating the Sproc: Basic Syntax<br />

Creating a sproc works pretty much the same as creating any other object in a database, except that it<br />

uses the AS keyword that you first saw when we took a look at views. The basic syntax looks like this:<br />

CREATE PROCEDURE|PROC <br />

[ [schema.] [VARYING] [= ] [OUT [PUT]]<br />

[READONLY]<br />

[, [schema.] [VARYING] [= ] [OUT[PUT]]<br />

[READONLY]<br />

[, ...<br />

...<br />

]]<br />

[WITH<br />

RECOMPILE| ENCRYPTION | [EXECUTE AS { CALLER|SELF|OWNER|}]<br />

[FOR REPLICATION]<br />

AS<br />

| EXTERNAL NAME ..<br />

As you can see, we still have our basic CREATE syntax that is the<br />

backbone of every CREATE statement. The only oddity here is the choice between PROCEDURE and PROC.<br />

Either option works just fine, but as always, I recommend that you be consistent regarding which one<br />

you choose (personally, I like the saved keystrokes of PROC). The name of your sproc must follow the<br />

rules for naming as outlined in Chapter 1.<br />

After the name comes a list of parameters. Parameterization is optional, and we’ll defer that discussion<br />

until a little later in the chapter.<br />

Last, but not least, comes your actual code following the AS keyword.<br />

An Example of a Basic Sproc<br />

368<br />

Perhaps the best example of basic sproc syntax is to get down to the most basic of sprocs — a sproc that<br />

returns all the columns in all the rows on a table — in short, everything to do with a table’s data.<br />

I would hope that by now you have the query that would return all the contents of a table down cold<br />

(Hint: SELECT * FROM...). If not, then I would suggest a return to the chapter on basic query syntax. In<br />

order to create a sproc that performs this basic query — we’ll do this one against the Employee table (in<br />

the HumanResources schema) — we just add the query in the code area of the sproc syntax:<br />

USE AdventureWorks<strong>2008</strong><br />

GO<br />

CREATE PROC spEmployee<br />

AS<br />

SELECT * FROM HumanResources.Employee;<br />

Not too rough, eh? If you’re wondering why I put the GO keyword in before the CREATE syntax (if we<br />

were just running a simple SELECT statement, we wouldn’t need it), it’s because most non-table CREATE<br />

statements cannot share a batch with any other code. Indeed, even with a CREATE TABLE statement, leaving<br />

out the GO can become rather dicey. In this case, having the USE command together with our CREATE<br />

PROC statement would have been a no-no and would have generated an error.

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

Saved successfully!

Ooh no, something went wrong!