The server raises error 1321 (ER_SP_NORETURNEND) when a stored FUNCTION reaches its END keyword without executing a RETURN statement.
MySQL Error 1321: ER_SP_NORETURNEND occurs when a stored FUNCTION finishes without a RETURN. Add a RETURN of the declared data type before the END or convert the routine to a PROCEDURE to fix the problem.
FUNCTION %s ended without RETURN
Error 1321 signals that a stored FUNCTION hit its closing END without returning a value. MySQL requires every execution path in a FUNCTION to supply a RETURN expression that matches the declared return type.
The server aborts execution, rolls back any uncommitted work done inside the function call, and sends SQLSTATE 2F005 to the client. Fixing the missing RETURN is critical because dependent queries cannot proceed until the routine compiles correctly.
The error is raised at create-time when you run CREATE FUNCTION or at run-time when control reaches END without RETURN because of conditional branches. MySQL versions 5.0+ through 8.3 exhibit identical behavior on this check.
Stored FUNCTIONS are designed to behave like expressions, so they must always yield a value. Procedures, by contrast, rely on OUT parameters and do not require RETURN. Mixing the two use cases triggers ER_SP_NORETURNEND.
Developers sometimes write business logic, then forget to append the final RETURN before END.
IF or CASE blocks may return a value only in certain branches, causing other paths to fall through to END.
FLOW control that exits the function can bypass RETURN if not carefully structured.
Porting code from a PROCEDURE template often omits the mandatory RETURN found in functions.
Raised when a UDF is mis-declared as a stored routine.
Appears when a BLOB or TEXT argument is used without length in a function.
Signals an improper RETURN found inside a PROCEDURE rather than a FUNCTION.
No. RETURN is only allowed in stored FUNCTIONS. Use OUT parameters or result sets in PROCEDURES.
No. LEAVE exits a block but does not deliver a value. You still need an explicit RETURN before END.
If SIGNAL terminates the function, a RETURN is not needed on that path, but all non-error paths must still return.
Galaxy’s real-time linting highlights missing RETURN statements as you type and its AI copilot autocompletes correct template code, preventing ER_SP_NORETURNEND.