Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Databases and Redis

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 show

Fix it fast

Most common cause, 1 of 4

A SELECT in the body has no INTO clause

Read the value into a local variable with SELECT ...

Fix
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 ;
Did this fix it?
Full steps for this cause: check, fix, verify

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.

  • Applies to MySQL 8.4 LTS, MySQL 9.7 LTS
  • Updated
  • By Maaz Zindani, Senior Software Engineer

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.

Run this first
grep -n -i -E "^[[:space:]]*(select|show|explain|check table)" create_order_total.sql

Then find the line you see in this list and open its fix:

  1. 1A SELECT in the body has no INTO clauseThe 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
  2. 2The body uses SHOW, EXPLAIN or CHECK TABLEEvery SELECT has INTO, yet the error remains, and the body contains a SHOW TABLES, SHOW VARIABLES, EXPLAIN or CHECK TABLE statement
  3. 3The routine is meant to return rows, so it has to be a procedureThe function's last SELECT returns a list of rows that the application wants, and RETURNS only holds a placeholder type
  4. 4A trigger body contains a plain SELECTThe 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 function
  • ERROR 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.

Check
grep -n -i "select" create_order_total.sql

Fix

Read the value into a local variable with SELECT ... INTO, then RETURN it. Remove SELECTs that were only there to print values while debugging.

Fix
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.

Verify
SELECT order_total(42);

Confirmed by dev.mysql.com

Did this fix it?

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.

Check
grep -n -i -E "show|explain|check table" create_order_total.sql

Fix

Ask information_schema for the same facts through SELECT ... INTO, or move maintenance statements such as CHECK TABLE out of the function entirely.

Fix
-- 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.

Verify
SHOW CREATE FUNCTION order_total\G

Confirmed by dev.mysql.com

Did this fix it?

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.

Check
SHOW FUNCTION STATUS WHERE Db = DATABASE();

Fix

Recreate the routine as a procedure and call it from the client or the application with CALL.

Fix
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.

Verify
CALL open_orders(7);

Confirmed by dev.mysql.com

Did this fix it?

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.

Check
grep -n -i "select" create_trigger.sql

Fix

Read the lookup into a local variable, then assign it to the NEW row.

Fix
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.

Verify
SHOW TRIGGERS LIKE 'order_items'\G

Confirmed by dev.mysql.com

Did this fix it?

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.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.