This error occurs when MySQL cannot locate a stored procedure or function you are trying to call. It is usually caused by a wrong database context, incorrect routine name, case sensitivity issues, or privilege problems.
When you execute a CALL statement for a stored procedure or use a stored function in a query, MySQL searches for that routine in the current database schema. Error 1305 (ER_SP_DOES_NOT_EXIST) means the server cannot find the routine by that name in that schema. This is MySQL's way of indicating that either: (1) the routine truly does not exist, (2) it exists in a different database than the one you are currently connected to, (3) the name or case does not match exactly, or (4) you lack EXECUTE permissions on that routine. It is important to distinguish between stored procedures (invoked with CALL) and stored functions (used in SELECT statements). Confusing the two will also trigger this error.
First, confirm you are using the right database:
SELECT DATABASE();
USE your_database_name;If you are connected to the wrong database, MySQL will search for the routine in that schema and not find it. Always ensure your session is in the correct database before calling a procedure.
Query the information_schema to verify the routine exists:
SELECT ROUTINE_NAME, ROUTINE_TYPE, ROUTINE_SCHEMA
FROM INFORMATION_SCHEMA.ROUTINES
WHERE ROUTINE_SCHEMA = DATABASE();Alternatively, use SHOW commands:
SHOW PROCEDURE STATUS WHERE db = DATABASE();
SHOW FUNCTION STATUS WHERE db = DATABASE();If the routine does not appear in the output, it either does not exist or is in a different database.
Ensure your CALL statement matches the exact name (including case) and syntax:
-- Correct (assuming proc_name exists)
CALL proc_name();
CALL proc_name(arg1, arg2);
-- Incorrect (spaces before parentheses)
CALL proc_name ();
CALL my_function (arg); -- Functions should use SELECT, not CALLRemember:
- Stored procedures are invoked with CALL
- Stored functions are used in SELECT statements or expressions
Using CALL on a function will fail with error 1305. Use: SELECT my_function(arg);
If the routine exists in a schema other than the current one, prefix the call with the database name:
CALL reporting.generate_report();
CALL analytics.process_data();This is especially useful in distributed environments or when working with multiple databases.
If the routine exists but you cannot execute it, verify your privileges:
-- Check what procedures a user can execute
SHOW GRANTS FOR 'username'@'hostname';
-- Grant EXECUTE privilege
GRANT EXECUTE ON PROCEDURE database_name.procedure_name TO 'username'@'hostname';
GRANT EXECUTE ON FUNCTION database_name.function_name TO 'username'@'hostname';
FLUSH PRIVILEGES;Without EXECUTE privilege, MySQL will report the routine as not existing (for security, it does not reveal whether the routine exists or if you simply lack permissions).
If the routine does not exist, you must recreate it. Retrieve the routine definition from source control or a backup:
DELIMITER //
CREATE PROCEDURE my_procedure(IN param1 INT, OUT result INT)
BEGIN
SELECT COUNT(*) INTO result FROM users WHERE id = param1;
END //
DELIMITER ;Then call it:
CALL my_procedure(1, @result);
SELECT @result;For deployments, ensure your SQL migration scripts include all CREATE PROCEDURE and CREATE FUNCTION statements.
On Unix-like systems, stored routine names are case-sensitive if lower_case_table_names=0. On Windows, they are case-insensitive.
-- Check the setting
SHOW VARIABLES LIKE 'lower_case_table_names';If lower_case_table_names=0, ensure your CALL statements match the routine name exactly:
-- If created as MyProcedure, must call as MyProcedure
CALL MyProcedure(); -- Correct
CALL myprocedure(); -- Error 1305 on UnixTo avoid this issue, always use lowercase for routine names across all platforms.
Replication and lag: In replicated environments, if a stored procedure was recently created on the primary, it may not yet exist on a replica due to replication lag. Check the replica's Seconds_Behind_Master status. If lag exists, either wait for replication to catch up or execute the CREATE PROCEDURE on the replica directly.
Database names with special characters: If your database name contains dots or other special characters, use backticks or fully qualified names to avoid parsing issues. For example, if your database is named "my.db", use: CALL \my.db\.my_procedure();
MySQL 8.0 and ALTER ROUTINE: MySQL 8.0 does not support CREATE OR REPLACE PROCEDURE. Instead, drop and recreate: DROP PROCEDURE IF EXISTS my_proc; CREATE PROCEDURE my_proc(...) ...
Connector-specific issues: Some database connectors or drivers (e.g., MySQL Connector/NET, PHP mysqli) have quirks with stored procedures. Always clear empty result sets returned by procedures before executing the next query to avoid "queries while other unbuffered queries are active" errors.
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