Error
Error Code:
1312
MariaDB Error 1312: Procedure Returns Unexpected Result Set
Description
This MariaDB error indicates that a stored procedure attempted to return a result set (e.g., from a SELECT statement) in a context where it is not allowed. This typically occurs when the procedure is invoked from within another database object like a trigger, function, or event that expects a single value or no result at all.
Error Message
PROCEDURE %s can't return a result set in the given context
Known Causes
4 known causesCalling from a TRIGGER
A stored procedure designed to return a result set is invoked by a database trigger, which is restricted to executing DML operations and cannot process result sets.
Calling from a User-Defined FUNCTION
A stored procedure that generates a result set is called from within a user-defined function, which is strictly designed to return a single scalar value.
Calling from an EVENT
An automated scheduled event attempts to execute a stored procedure that returns a result set, but the event's execution context does not support handling such output.
Nested Procedure Context Mismatch
A stored procedure returning a result set is called from another stored procedure that does not expect or cannot handle a result set from the nested call.
Solutions
3 solutions available1. Remove Unnecessary SELECT Statements in Stored Procedures easy
Eliminate SELECT statements within stored procedures that are not intended to return data to the caller.
1
Identify the stored procedure causing the error. The error message will usually indicate the procedure name.
2
Examine the stored procedure's code for any `SELECT` statements that are not part of a `SELECT INTO` or a statement intended to be returned as a result set (e.g., within a cursor).
3
If a `SELECT` statement is present but not needed for the procedure's logic or for returning a result set, remove it. If it's for debugging, comment it out.
DELETE FROM your_table WHERE some_condition;
-- SELECT * FROM another_table WHERE id = some_id; -- Commented out for debugging
4
Recompile or re-execute the stored procedure to apply the changes.
DELIMITER //
CREATE OR REPLACE PROCEDURE your_procedure_name(...)
BEGIN
-- Your modified procedure logic here
END //
DELIMITER ;
CALL your_procedure_name(...);
2. Modify Calling Context to Accept Result Sets medium
Adjust the client application or script that calls the stored procedure to handle expected result sets.
1
Understand how the stored procedure is being called. Is it from a client application (e.g., Python, PHP, Java), a script, or another stored procedure?
2
If calling from a client application, consult the documentation for your database connector library. You'll need to implement logic to fetch and process any result sets returned by the procedure.
Example in Python with `mysql.connector`:
python
cursor = connection.cursor()
cursor.callproc('your_procedure_name', args)
# Fetch all result sets if multiple are expected
for result in cursor.stored_results():
data = result.fetchall()
print(data)
connection.commit() # If the procedure performs DML
cursor.close()
3
If calling from another stored procedure, ensure the calling procedure has a mechanism to handle a result set, such as using cursors or `SELECT INTO` if a single row is expected. If the result set is not intended, modify the called procedure as per Solution 1.
Example of a procedure calling another procedure that returns a result set:
sql
DELIMITER //
CREATE PROCEDURE calling_procedure()
BEGIN
DECLARE var1 INT;
DECLARE var2 VARCHAR(255);
-- Call the procedure that returns a result set
CALL procedure_that_returns_results(@out1, @out2);
-- You might need to fetch the results if they are intended
-- For example, if procedure_that_returns_results returns a single row:
-- SELECT @out1, @out2;
-- Or if it returns multiple rows, you'd need a cursor.
END //
DELIMITER ;
3. Explicitly Define Return Values for Procedures medium
Use `OUT` or `INOUT` parameters to return specific values instead of relying on implicit result sets.
1
Identify the values that the stored procedure is intended to communicate back to the caller. These might currently be exposed via `SELECT` statements.
2
Modify the stored procedure definition to include `OUT` or `INOUT` parameters for these values.
Example: Change from `SELECT COUNT(*) INTO @count FROM ...` to an OUT parameter.
Original (problematic):
sql
DELIMITER //
CREATE PROCEDURE get_count()
BEGIN
SELECT COUNT(*) FROM my_table;
END //
DELIMITER ;
Modified:
sql
DELIMITER //
CREATE PROCEDURE get_count_with_out_param(OUT result_count INT)
BEGIN
SELECT COUNT(*) INTO result_count FROM my_table;
END //
DELIMITER ;
3
Update the calling code to pass variables to receive these `OUT` or `INOUT` parameters.
Example when calling the modified procedure:
sql
SET @my_count_var = 0; -- Initialize variable
CALL get_count_with_out_param(@my_count_var);
SELECT @my_count_var; -- Now you can access the count
4
Remove any `SELECT` statements from the procedure that were solely intended to return these values, as they are now handled by the parameters.