CREATE FUNCTION or CREATE TRIGGER refuses a SELECT in the body
Your error
ERROR 1415 (0A000): Not allowed to return a result set from a function1 more line it can showFix it fast
Most common cause, 1 of 4A SELECT in the body has no INTO clause
Read the value into a local variable with SELECT ...
DELIMITER //
CREATE FUNCTION order_total(p_order_id INT) RETURNS DECIMAL(10,2)
READS SQL DATA
BEGIN
DECLARE v_total DECIMAL(10,2);
SELECT SUM(qty * price) INTO v_total
FROM order_items WHERE order_id = p_order_id;
RETURN v_total;
END //
DELIMITER ;Short answer
ERROR 1415: Not allowed to return a result set from a function means the body holds a statement that sends rows to the client: a SELECT without INTO, or SHOW, EXPLAIN or CHECK TABLE. Use SELECT ... INTO and RETURN one value, or make the routine a procedure run with CALL. Triggers share the rule. MySQL 8.4 and 9.7.
Which cause do you have?
List every statement in the routine's source file that can produce rows: SELECT lines that have no INTO, and SHOW, EXPLAIN or CHECK TABLE. Each line printed is a candidate.
grep -n -i -E "^[[:space:]]*(select|show|explain|check table)" create_order_total.sqlThen find the line you see in this list and open its fix:
- 1A SELECT in the body has no INTO clause
The error names a function, and its body contains a SELECT whose values are not stored in variables, such as a SELECT left in for debugging - 2The body uses SHOW, EXPLAIN or CHECK TABLE
Every SELECT has INTO, yet the error remains, and the body contains a SHOW TABLES, SHOW VARIABLES, EXPLAIN or CHECK TABLE statement - 3The routine is meant to return rows, so it has to be a procedure
The function's last SELECT returns a list of rows that the application wants, and RETURNS only holds a placeholder type - 4A trigger body contains a plain SELECT
The message ends with from a trigger, and CREATE TRIGGER is the failing statement
What does the error look like?
ERROR 1415 (0A000): Not allowed to return a result set from a functionERROR 1415 (0A000): Not allowed to return a result set from a trigger
Cause 1 of 4
A SELECT in the body has no INTO clause
You seeThe error names a function, and its body contains a SELECT whose values are not stored in variables, such as a SELECT left in for debugging
Check
MySQL raises Not allowed to return a result set from a function when the body has a SELECT without INTO, because that statement would hand a result set to the caller. Stored functions may only return their single RETURN value. The manual lists SELECT without an INTO var_list among the statements a function cannot contain.
grep -n -i "select" create_order_total.sqlFix
Read the value into a local variable with SELECT ... INTO, then RETURN it. Remove SELECTs that were only there to print values while debugging.
DELIMITER //
CREATE FUNCTION order_total(p_order_id INT) RETURNS DECIMAL(10,2)
READS SQL DATA
BEGIN
DECLARE v_total DECIMAL(10,2);
SELECT SUM(qty * price) INTO v_total
FROM order_items WHERE order_id = p_order_id;
RETURN v_total;
END //
DELIMITER ;Verify
The function is created and returns one value per call.
SELECT order_total(42);Confirmed by dev.mysql.com
Cause 2 of 4
The body uses SHOW, EXPLAIN or CHECK TABLE
You seeEvery SELECT has INTO, yet the error remains, and the body contains a SHOW TABLES, SHOW VARIABLES, EXPLAIN or CHECK TABLE statement
Check
The ban covers every statement that returns rows, not only SELECT. The restrictions page names SHOW, EXPLAIN and CHECK TABLE explicitly.
grep -n -i -E "show|explain|check table" create_order_total.sqlFix
Ask information_schema for the same facts through SELECT ... INTO, or move maintenance statements such as CHECK TABLE out of the function entirely.
-- instead of SHOW TABLES LIKE 'order_items'
SELECT COUNT(*) INTO v_found
FROM information_schema.TABLES
WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'order_items';Verify
CREATE FUNCTION succeeds, and the function still reaches the same decision.
SHOW CREATE FUNCTION order_total\GConfirmed by dev.mysql.com
Cause 3 of 4
The routine is meant to return rows, so it has to be a procedure
You seeThe function's last SELECT returns a list of rows that the application wants, and RETURNS only holds a placeholder type
Check
A function returns one value. Statements that return a result set are allowed in stored procedures, so a routine whose job is to hand back rows belongs there. A function that only needs to walk over rows can do it with a cursor and FETCH.
SHOW FUNCTION STATUS WHERE Db = DATABASE();Fix
Recreate the routine as a procedure and call it from the client or the application with CALL.
DELIMITER //
CREATE PROCEDURE open_orders(IN p_customer_id INT)
READS SQL DATA
BEGIN
SELECT id, total, created_at
FROM orders
WHERE customer_id = p_customer_id AND status = 'open';
END //
DELIMITER ;Verify
CALL returns the rows as a normal result set.
CALL open_orders(7);Confirmed by dev.mysql.com
Cause 4 of 4
A trigger body contains a plain SELECT
You seeThe message ends with from a trigger, and CREATE TRIGGER is the failing statement
Check
The restrictions for stored functions also apply to triggers, and the same message names the trigger instead of the function. A trigger that looks up a value for the new row needs the value in a variable, not a result set.
grep -n -i "select" create_trigger.sqlFix
Read the lookup into a local variable, then assign it to the NEW row.
DELIMITER //
CREATE TRIGGER order_items_bi BEFORE INSERT ON order_items
FOR EACH ROW
BEGIN
DECLARE v_price DECIMAL(10,2);
SELECT price INTO v_price FROM products WHERE id = NEW.product_id;
SET NEW.price = v_price;
END //
DELIMITER ;Verify
An insert into order_items stores the price taken from products.
SHOW TRIGGERS LIKE 'order_items'\GConfirmed by dev.mysql.com
How do I stop it coming back?
- Keep each stored function to one job: compute and RETURN one value; put routines that list rows in procedures.
- Debug stored functions with a separate test procedure or with SELECT on the function's result, not with SELECT statements inside the function.
- Run the CREATE scripts for routines and triggers in CI against the same MySQL version as production, so error 1415 fails the build first.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine on the same stack: TaxiDex, QistLock, ShopGo.