MySQL error 1305 occurs when you try to call a stored function that doesn't exist in the database. This commonly happens due to spaces between the function name and parentheses, incorrect database context, misspelled function names, or calling a stored procedure as a function.
In MySQL, functions and stored procedures are distinct objects. A function must be called using SELECT statements and returns a value, while a procedure is called with CALL. Error 1305 (ER_SP_DOES_NOT_EXIST) indicates that MySQL cannot locate the requested function in the specified database schema. This error occurs at execution time when MySQL searches the current database (or specified database) for the function definition. If the function name doesn't match exactly—including case sensitivity on Unix systems, spelling, or if it's in a different database—MySQL throws this error.
Ensure you are connected to the correct database:
SELECT DATABASE();If the result is NULL or shows a different database, switch to the correct one:
USE your_database;Always include an explicit USE statement before calling functions in production scripts.
Query the information schema to verify the function exists:
SELECT ROUTINE_NAME, ROUTINE_TYPE FROM information_schema.ROUTINES
WHERE ROUTINE_SCHEMA = "your_database";Or use the MySQL shorthand:
SHOW FUNCTION STATUS WHERE Db = "your_database";Note the exact spelling and case of the function name. If the function is not listed, it needs to be created.
The most common cause is a space between the function name and the opening parenthesis. MySQL interprets this as a syntax error.
Incorrect:
SELECT IFNULL (value, default_value) FROM table;
SELECT CONCAT (col1, col2) FROM table;Correct:
SELECT IFNULL(value, default_value) FROM table;
SELECT CONCAT(col1, col2) FROM table;Update all function calls to remove the space before the parenthesis.
If the function exists but error 1305 still occurs, try calling it with the full qualified name:
SELECT database.function_name(arguments) FROM table;For example:
SELECT mydb.calculate_price(quantity, rate) FROM orders;This ensures MySQL searches the correct database schema, bypassing any context confusion.
Confirm that you are calling a function in a SELECT statement, not a procedure. Procedures are called with CALL:
Function (returns a value, used in SELECT):
SELECT my_function(col1) FROM table;Procedure (does not return a value, uses CALL):
CALL my_procedure(arg1, arg2);If error 1305 mentions a procedure, switch to CALL syntax. If it mentions a function but you defined a procedure, recreate it as a function.
On Unix systems with lower_case_table_names=0, function names are case-sensitive. Check your MySQL configuration:
SHOW VARIABLES LIKE 'lower_case_table_names';If it returns 0 (case-sensitive), ensure your function call matches the exact case used in the CREATE FUNCTION statement:
-- Created as:
CREATE FUNCTION CalculatePrice() ...
-- Must be called as:
SELECT CalculatePrice(...);
-- Not:
SELECT calculateprice(...);On Windows (lower_case_table_names=1) and macOS (lower_case_table_names=2), function names are case-insensitive.
The current user may lack EXECUTE privilege on the function. Check permissions:
SELECT PRIVILEGE_TYPE FROM information_schema.ROLE_TABLE_GRANTS
WHERE TABLE_NAME = "your_database";Grant EXECUTE privilege if missing:
GRANT EXECUTE ON FUNCTION your_database.my_function TO "app_user"@"localhost";
FLUSH PRIVILEGES;Verify the user can execute the function:
SELECT my_function(test_arg);If the function does not exist, create it from your application source code or schema definition:
CREATE FUNCTION my_function(param1 INT) RETURNS INT
DETERMINISTIC
BEGIN
RETURN param1 * 2;
END;Include the DETERMINISTIC keyword for functions that return the same output for the same input. This enables MySQL to cache results.
After creating the function, verify it works:
SELECT my_function(5);After fixing the function call, test it in the environment where error 1305 occurred:
USE your_database;
SELECT my_function(argument) FROM table LIMIT 1;If using a connection pool or ORM (like Sequelize, SQLAlchemy), ensure the connection is established to the correct database before executing the query.
In application code:
// Node.js with mysql2
const result = await connection.query(
"USE mydb; SELECT my_function(?) as result",
[arg]
);Function name resolution: MySQL searches functions in the current database first. If a function shares the same name as a built-in function (like "substring"), always qualify the custom function: "database.my_substring()". Built-in functions are tried first, which can cause confusion.
Replicated environments: On master-slave replication, if the slave function creation fails, the slave will throw error 1305 when replication tries to execute the function. Check the slave status with "SHOW SLAVE STATUS;" and manually create missing functions on the slave.
For containerized MySQL: If using Docker or Kubernetes, ensure the initialization scripts (init.sql) include CREATE FUNCTION statements. Functions do not persist across container restarts unless created by the init process or stored in a mounted volume.
Performance: Limit use of functions in SELECT clauses on large result sets, as functions are called for every row. Use stored procedures with result tables or window functions for better performance.
ERROR 1064: You have an error in your SQL syntax
How to fix "ERROR 1064: You have an error in your SQL syntax" in MySQL
ERROR 1054: Unknown column in field list
Unknown column in field list
ER_WINDOW_RANGE_FRAME_NUMERIC_TYPE (3589): RANGE frame requires numeric ORDER BY expression
RANGE frame requires numeric ORDER BY expression in MySQL window functions
CR_ALREADY_CONNECTED (2058): Handle already connected
How to fix "CR_ALREADY_CONNECTED (2058): Handle already connected" in MySQL
ER_WINDOW_DUPLICATE_NAME (3591): Duplicate window name
How to fix ER_WINDOW_DUPLICATE_NAME (3591) in MySQL window functions