To create a stored procedure, you need to give the procedure a unique name and then write the sequence of SQL statements to be included in the procedure. The following is the basic syntax for creating stored procedures: create proc [owner.]procedure_name[; number] [[(]@parm_name datatype = default_value [output] [, ... ] [)]] [with {recompile encryption}] as SQL Statements [return [integer_status_value]] It is good programming practice to always end a procedure with the RETURN statement and to specify a return status other than 0 when an error condition occurs. Listing 28.1 shows a simple stored procedure. Listing 28.1 A Stored Procedure That Returns Book Titles and the Names of the Authors Who Wrote ThemCREATE PROCEDURE dbo.title_authors AS SELECT a.au_lname, a.au_fname, t.title FROM titles t INNER JOIN titleauthor ta ON t.title_id = ta.title_id RIGHT OUTER JOIN authors a ON ta.au_id = a.au_id RETURN
To execute a stored procedure, simply invoke it by its name (the same way you probably have already executed system stored procedures, such as sp_help ). If the procedure isn't the first statement in a batch, precede the procedure name with the EXEC keyword. See Chapter 26, "Using Transact-SQL in SQL Server 2000," for more information on batches. The following is the basic syntax for executing stored procedures: [exec[ute]] [@status =] [owner.]procedure_name[; number] [[@parm_name =] expression [output][, ... ]] [with recompile]
Stored Procedure GroupsIf you create a procedure with a name that already exists, you receive an error message that might not be expected. Let's try to create a procedure with the same name as a procedure that already exists, title_authors : CREATE PROCEDURE title_authors AS SELECT a.au_lname, a.au_fname, t.title FROM titles t INNER JOIN titleauthor ta ON t.title_id = ta.title_id RIGHT OUTER JOIN authors a ON ta.au_id = a.au_id WHERE a.state = 'CA' RETURN Server: Msg 2714, Level 16, State 5, Procedure title_authors, Line 7 There is already an object named 'title_authors' in the database. SQL Server does provide the capability of having multiple procedures created under the same name, but with different group numbers . To create a procedure with a different group number, add a semicolon ( ; ) and an integer as shown in Listing 28.2. Listing 28.2 Creating a Stored Procedure with a Group NumberCREATE PROCEDURE title_authors;2 AS SELECT a.au_lname, a.au_fname, t.title FROM titles t INNER JOIN titleauthor ta ON t.title_id = ta.title_id RIGHT OUTER JOIN authors a ON ta.au_id = a.au_id WHERE a.state = 'CA' RETURN GO EXEC title_authors;2 The first procedure created will automatically get an implicit group number of 1. If no group number is specified when executing a stored procedure, SQL Server will execute the stored procedure with group number 1 by default. Stored procedure groups are an odd feature in SQL Server, and they have limited usefulness . While you can drop all procedures within a group by excluding the group number with the DROP PROCEDURE statement, you cannot drop a single procedure within a group. One potential use of grouping functionality is for version handling. For each new version of the procedure, you could add to the grouping number rather than creating a new procedure with a different name. The original stored procedure will still be available for clients that have not yet been modified to use the newer version of the stored procedure. Updated clients can execute the new version of the stored procedure by including the new group number. |