Addressing MySQL Error 1128: Function Not Defined

Encountering Error 1128 – SQLSTATE: HY000 (ER_FUNCTION_NOT_DEFINED) in MySQL can be a roadblock when working with stored functions. This error message indicates that a function you’re attempting to invoke does not exist in the current database context. It’s important to not only understand why this error occurs but also to know how to fix it effectively. In this guide, we’ll walk through various scenarios that may cause this error and provide examples and sample code to help you resolve it.

Understanding Error 1128

Error 1128 occurs when you call a stored function that MySQL cannot find in the database. This could be due to:

  • A typo in the function name.
  • The function not being created in the database.
  • The function being deleted or not correctly migrated.
  • Incorrect database context where the function does not exist.

Diagnosing the Issue

First, verify that the function name is spelled correctly. If the name is correct, check that the function exists in the database by using the SHOW FUNCTION STATUS command:

SHOW FUNCTION STATUS WHERE Name = 'your_function_name';

If the function does not appear in the results, it confirms that the function is not defined in the current database.

Resolving the Error

Here are several methods to fix Error 1128, with examples and sample code:

1. Creating the Missing Function

If the function does not exist, you need to create it using the CREATE FUNCTION statement.



CREATE FUNCTION CalculateDiscount(price DECIMAL(10,2), discount_rate DECIMAL(5,2))
  RETURN price - (price * discount_rate / 100);


2. Correcting the Function Name

Correct any typographical errors in the function name in your SQL statements.


Incorrect function call:

SELECT CaluclateDiscount(100.00, 15.00); -- 'CaluclateDiscount' is a typo

Correct function call:

SELECT CalculateDiscount(100.00, 15.00);

3. Verifying Database Context

Make sure you are calling the function from the correct database. If necessary, qualify the function with the database name.


SELECT your_database_name.CalculateDiscount(100.00, 15.00);

4. Checking Function Permissions

Ensure that the user has the necessary permissions to execute the function. Grant execute permission if needed.


GRANT EXECUTE ON FUNCTION your_database_name.CalculateDiscount TO 'your_user'@'your_host';

5. Restoring Deleted Functions

If the function was accidentally deleted, restore it from a backup or recreate it using the CREATE FUNCTION statement as shown earlier.

6. Migrating Functions Correctly

When moving databases, ensure that all stored functions are correctly migrated. Use mysqldump with the --routines flag to include stored procedures and functions.


mysqldump --routines -u username -p database_name > backup.sql


MySQL Error 1128 is a clear indication of a discrepancy between the expected and actual functions available in your database. By checking for typos, verifying the existence of the function, ensuring correct database context, checking permissions, restoring deleted functions, and correctly migrating functions, you can overcome this error. Always keep a backup of your database routines and verify them after any significant changes to the database structure.

For more information on managing stored functions in MySQL, refer to the MySQL documentation on creating and using stored functions.

Leave a Comment