Part One of this exercise will show you how to:
- Create a simple stored procedure.
- Execute the stored procedure.
Directions
To create a stored procedure called prGetCustomers that simply returns all rows in the Customers table:
- Locate or create the Northwinds database.
- Open PhpMyAdmin (or work from the command line interface).
- Be sure to select the Northwinds database from the left panel, or preface each of the following statements with the statement "USE northwind;"
-
Type the following
DELIMITER //
CREATE PROCEDURE prGetCustomers ()
BEGIN
SELECT * FROM customers;
END //
DELIMITER ; -
Now, execute this stored procedure. Type
CALL prGetCustomers ();
- The results will be displayed in the lower window.
- Click here for a Getting Started with MySQL Stored Procedures tutorial.
Part Two of this exercise requires you to practice passing parameters to a stored procedure.
-
Enter the following code:
DELIMITER //
CREATE PROCEDURE prGetCustomersFromCountry (IN countryName VARCHAR(15))
BEGIN
SELECT CompanyName
FROM customers
WHERE Country = countryName;
END //
DELIMITER ; -
Now, execute this stored procedure. Type
CALL prGetCustomersFromCountry ('Canada');
- Practice writing CountOrderByStatus and Capitalize from the Stored Procedure Parameters tutorial. Note that there is a link to their database in Step 4 of the Start Here tab, or you can download it here.
Play around with the other stored procedures features at MySQL Stored Procedures tutorial.