Creating a stored procedure

A stored procedure is a named collection of SQL statements and flow control statements. After you create a stored procedure, you can use it repeatedly without the need to enter the SQL statements individually each time you want to repeat a procedure.

A stored procedure that performs a select, execute, or data modification command must be owned by the same user as the object acted upon.

Privileges

Only a database owner or a user or group with create procedure permission can create a stored procedure.

StepsCreating a stored procedure

  1. In the database hierarchy, select the Stored Procedures folder.

  2. Select File | New | Procedure.

  3. The New Procedure Creation wizard opens.

The New Procedure Creation wizard asks for the following information:

Table 20-1: Inputs to new procedure creation wizard

Input

Description

Name

Name for the new stored procedure.

Owner

The user who owns the stored procedure.

Recompile

Whether or not to recompile each time the stored procedure is run.

Group

Whether or not the stored procedure is to be part of a group. If so, the group number.

Code

Enter the code for the stored procedure in the editor.

Recompile

Select the recompile option if you expect that the execution of the stored procedure may be different each time. For example, recompile if the data passed in its parameters changes so much that a query plan produced at execution would differ greatly from a plan that is stored.

Shortcut

Select the Stored Procedures folder in the appropriate database. In the right pane, double-click the Add Procedure icon.