DEV Community

shrileela
shrileela

Posted on

Unable to Return Multiple Rows from a MySQL Function

I am trying to create a MySQL function that returns multiple rows, but I am unable to achieve the expected result. Here’s what I attempted:

DELIMITER $$

CREATE FUNCTION FUN_GET_ORDERS_BY_CUSTOMER(p_customer_id INT)
RETURNS TABLE
AS

RETURN

SELECT 
order_id, 
order_date, 
total_amount 
FROM 
orders 
WHERE customer_id = p_customer_id; 
Enter fullscreen mode Exit fullscreen mode

$$

DELIMITER ;

But this gives me a syntax error because MySQL functions don’t support returning tables directly like in some other databases.

What would be the best way to rewrite this function to return multiple rows?

Any guidance or best practices would be appreciated!

Top comments (0)