Common SQL Errors

MySQL Error 1321 ER_SP_NORETURNEND: FUNCTION ended without RETURN - Causes and Fixes

Galaxy Team
August 6, 2025

The server raises error 1321 (ER_SP_NORETURNEND) when a stored FUNCTION reaches its END keyword without executing a RETURN statement.

Sign up for the latest in common SQL errors from the Galaxy Team!
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

What is MySQL error code 1321?

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.

Error Highlights

Typical Error Message

FUNCTION %s ended without RETURN

Error Type

Syntax Error

Language

MySQL

Symbol

ER_SP_NORETURNEND

Error Code

1321

SQL State

2F005

Explanation

Table of Contents

What is MySQL Error 1321 ER_SP_NORETURNEND?

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.

When does this error appear?

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.

Why is a RETURN mandatory in MySQL functions?

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.

Common Causes

Missing RETURN in main code path

Developers sometimes write business logic, then forget to append the final RETURN before END.

Conditional branches without default return

IF or CASE blocks may return a value only in certain branches, causing other paths to fall through to END.

Exception or LEAVE handler skips RETURN

FLOW control that exits the function can bypass RETURN if not carefully structured.

Copy-pasting procedure code

Porting code from a PROCEDURE template often omits the mandatory RETURN found in functions.

Related Errors

Error 1338 ER_SP_UDF_FUNCTION

Raised when a UDF is mis-declared as a stored routine.

Error 1415 ER_SP_BLOB_ARG

Appears when a BLOB or TEXT argument is used without length in a function.

Error 1303 ER_SP_RETURN_IN_SP

Signals an improper RETURN found inside a PROCEDURE rather than a FUNCTION.

FAQs

Can I use RETURN in a PROCEDURE?

No. RETURN is only allowed in stored FUNCTIONS. Use OUT parameters or result sets in PROCEDURES.

Does LEAVE implicitly return a value?

No. LEAVE exits a block but does not deliver a value. You still need an explicit RETURN before END.

Is RETURN required after a SIGNAL?

If SIGNAL terminates the function, a RETURN is not needed on that path, but all non-error paths must still return.

How does Galaxy help avoid this error?

Galaxy’s real-time linting highlights missing RETURN statements as you type and its AI copilot autocompletes correct template code, preventing ER_SP_NORETURNEND.

Start Querying with the Modern SQL Editor Today!
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

Check out some other errors

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo