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.
Only a database owner or a user or group with create procedure permission can create a stored procedure.
Creating a stored procedure
In the database hierarchy, select the Stored Procedures folder.
Select File | New | Procedure.
The New Procedure Creation wizard opens.
The New Procedure Creation wizard asks for the following information:
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. |
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.
Select the Stored Procedures folder in the appropriate database. In the right pane, double-click the Add Procedure icon.