Stored Function
Stored Function
A stored function is a specialized type of stored program designed to return a single value.
Typically, you use stored functions to encapsulate common formulas or business rules,
making them reusable across SQL statements or other stored programs.
To create a stored function, you use the CREATE FUNCTION statement. The following
illustrates the basic syntax for creating a new stored function
DELIMITER $$
DELIMITER ;
Syntax explaination
First, specify the name of the stored function that you want to create after CREATE FUNCTION
keywords.
Second, list all parameters of the stored function inside the parentheses followed by the
function name.
By default, stored functions consider all parameters as IN parameters. You cannot specify IN
, OUT or INOUT modifiers to parameters
Third, specify the data type of the return value in the RETURNS statement, which can be any
valid data type.
Page 1 of 5
Fourth, determine whether a function is deterministic or not using the DETERMINISTIC
keyword.
A deterministic function always returns the same result for the same input parameters, while
a non-deterministic function produces different results for the same input parameters.
If you don’t use DETERMINISTIC or NOT DETERMINISTIC, MySQL defaults to the NOT
DETERMINISTIC option.
Finally, write the code in the body of the stored function in the BEGIN...END block.
Inside the body section, you need to include at least one RETURN statement. The RETURN
statement sends a value to the calling programs.
Upon reaching the RETURN statement, the stored function terminates the execution of the
stored function immediately.
Page 2 of 5
The following CREATE FUNCTION statement creates a function that returns the customer
level based on credit.
Answer
DELIMITER $$
USE [database_name] $$
CREATE FUNCTION CustomerLevel(
credit DECIMAL(10,2)
)
RETURNS VARCHAR(20)
DETERMINISTIC
BEGIN
DECLARE customerLevel VARCHAR(20);
DELIMITER ;
Once the function is created, you can view it in MySQL Workbench under the Functions
section.
Or, you can view all stored functions in the current classicmodels database by using the
SHOW FUNCTION STATUS as follows
The following statement illustrates how to call the CustomerLevel stored function
SELECT
customerName,
CustomerLevel(creditLimit)
FROM
customers
ORDER BY
customerName;
Example
1. A stored function calculates the total price of an order, including tax, based on the
unit price, quantity, and a tax rate. Use the rate of 0.05
Page 4 of 5
Step 1: Create the stored functions
DELIMITER //
CREATE FUNCTION
calculate_total_price(unit_price DECIMAL(10,2), quantity INT, tax_rate DECIMAL(5,2))
RETURNS DECIMAL(10,2)
DETERMINISTIC
BEGIN
RETURN (unit_price * quantity) * (1 + tax_rate);
END //
DELIMITER ;
SELECT
order_id,
product_name,
unit_price,
quantity,
calculate_total_price(unit_price, quantity, 0.05) AS total_price_with_tax
FROM
orders;
Example
From a your student table, create a stored function that returns “KE” if the student is female
and “ME” if the student if male, the use it to return the list of student with their respective
gender.
Page 5 of 5