BLOGS BY TOPIC▼
BLOGS BY AUTHOR▼
BLOGS BY YEAR▼
Stored procedures are programs in SQL which allow you to pass parameters to queries and manipulate sets of rows in tables. Find out how to write them with this online training blog!
- Stored Procedures in SQL
- Creating Simple Stored Procedures
- Running Stored Procedures (this blog)
- Modifying or Changing a Stored Procedure
- Passing Parameters to Stored Procedures
- A Worked Example of a Longer Stored Procedure
Posted by Andy Brown on 24 May 2012
You need a minimum screen resolution of about 700 pixels width to see our blogs. This is because they contain diagrams and tables which would not be viewable easily on a mobile phone or small laptop. Please use a larger tablet, notebook or desktop computer, or change your screen resolution settings.
Running Stored Procedures
Let's suppose that you've created the stored procedure described in the previous part of this blog:
CREATE PROC spListActors
-- display list of actors
You now want to run the spListActors stored procedure, to list out its contents. Below are 3 ways to do this.
Methood 1 - Selecting the Stored Procedure Name
The simplest way to run a stored procedure is just to select its name and press F5:
Just double-click on the name of the stored procedure to select it, and then execute it like any other SQL query.
This will produce the results of the SELECT statement included in the procedure:
The first few rows returned by running the procedure.
Note that this method doesn't work if your stored procedure takes parameters (then again, we haven't covered parameters yet!).
Method 2 - Right-Clicking on the Procedure Name
A second way to run a stored procedure is to right-click on its name:
Right-click on the name of the procedure in SQL Server Management Studio and choose to execute it as shown here.
Again, this method doesn't work well if your stored procedure takes parameters.
Method 3 - Execute the Stored Procedure in a Query
The third way to run a stored procedure is to use the EXECUTE command within a query:
-- 3 ways to run procedure to list out actors
As the above examples show, you can abbreviate EXECUTE to EXEC or - under certain circumstances - miss it out altogether.