How to create and execute MySQL stored functions and procedures

This article describes how to create and execute MySQL stored functions and procedures on your A2 Hosting account. You can use stored functions and procedures for a wide range of scenarios. For example, well-designed stored functions and procedures can enhance database security, improve data integrity, and increase performance.

Stored functions

MySQL stored functions provide a powerful and flexible way to manipulate and process data. You can define and run stored functions on any A2 Hosting server that uses MySQL.

Setting up a test database

To demonstrate a basic example of stored functions, let's start by creating a database that we can use for testing purposes. In the following SQL statement, replace username with your account username:

CREATE DATABASE username_test;
You can run the previous SQL command (and the following SQL commands) from the command line using the MySQL tool, or in your web browser using phpMyAdmin.

If you are using phpMyAdmin, click the name username_test to select the database. Otherwise, if you are using the mysql command-line program, type the following SQL statement:

USE username_test;

Next, create a table in the username_test database named products. To do this, run the following SQL statement:

CREATE TABLE products (prod_id INT NOT NULL AUTO_INCREMENT, prod_name VARCHAR(20) NOT NULL, prod_cost FLOAT NOT NULL DEFAULT 0.0, prod_price FLOAT NOT NULL DEFAULT 0.0, PRIMARY KEY(prod_id));

The following SQL statement adds some sample data to the products table:

INSERT INTO products (prod_name, prod_cost, prod_price) VALUES ('Basic Widget',5.95,8.35),('Micro Widget',0.95,1.35),('Mega Widget',99.95,140.00);
Creating the stored function

Now that we have a database and a table to work with, we are ready to create a stored function. Let's create a function named calcProfit. This function takes two input parameters: the cost and the price of something. It calculates the profit by subtracting the cost from the price, and then returns the value to the calling expression.

To create this stored function, run the following MySQL statements:

DELIMITER $$
CREATE FUNCTION calcProfit(cost FLOAT, price FLOAT) RETURNS DECIMAL(9,2)
BEGIN
  DECLARE profit DECIMAL(9,2);
  SET profit = price-cost;
  RETURN profit;
END$$
DELIMITER ;
The DELIMITER command at the beginning of these statements prevents MySQL from processing the function definition too soon. The DELIMITER command at the end of these statements returns processing to normal.
Using the stored function

You can now execute the stored function in a database query. The following SQL statement demonstrates how to do this:

SELECT *, calcProfit(prod_cost,prod_price) AS profit FROM products;

This SQL statement returns the following results:

+---------+--------------+-----------+------------+--------+
| prod_id | prod_name    | prod_cost | prod_price | profit |
+---------+--------------+-----------+------------+--------+
|       1 | Basic Widget |      5.95 |       8.35 |   2.40 |
|       2 | Micro Widget |      0.95 |       1.35 |   0.40 |
|       3 | Mega Widget  |     99.95 |        140 |  40.05 |
+---------+--------------+-----------+------------+--------+

As you can see, the calcProfit function automatically calculates the profit (price minus the cost) for each product in the table.

Stored procedures

Stored procedures are sometimes confused with stored functions, but they are different in some important ways. Stored procedures, for example, must be invoked with the CALL statement, whereas stored functions can be used directly in SQL expressions. You can define and run stored procedures on any A2 Hosting server that uses MySQL.

The following MySQL statements demonstrate how to create a very basic stored procedure named procedureTest. This procedure performs a simple lookup on the products table that we used in the stored function example above. Although this procedure does not have much practical use, it demonstrates the correct syntax and structure for declaring a stored procedure:

DELIMITER $$
CREATE PROCEDURE procedureTest()
BEGIN
  SELECT prod_name FROM products;
END$$
DELIMITER ;

To execute the stored procedure, use the following MySQL statement:

CALL procedureTest() \G
If you are using phpMyAdmin, type the previous MySQL statement without the \G option at the end.

More Information

For more information about stored procedures and functions in MySQL, please visit http://dev.mysql.com/doc/refman/5.0/en/create-procedure.html.

Get MySQL Hosting

Article Details

Did you find this article helpful? Then you'll love our support. Experience the A2 Hosting difference today and get a pre-secured, pre-optimized website. Check out our web hosting plans today.

We use cookies to personalize the website for you and to analyze the use of our website. You consent to this by clicking on "I consent" or by continuing your use of this website. Further information about cookies can be found in our Privacy Policy.