I have a use case that requires the creation of a temporary table with a unique name from within a stored procedure. The only way I have found to execute queries with a dynamic table name is to prepare the query:
SET @d=CONCAT('SELECT * FROM table_', case);
PREPARE q from @d;
EXECUTE q;
This works well enough, but I cannot figure out how to return the result set. Any ideas?
You don’t have to do anything extra to return the result set. It’s enough to EXECUTE the prepared query. The result set of that query execution will automatically become the result set of the stored procedure.
Re your comment: it might be this bug: http://bugs.mysql.com/bug.php?id=20028 which was fixed.
You may correct the issue by upgrading.
Also see https://dba.stackexchange.com/questions/7499/function-returning-no-data-zero-rows-fetched-selected-or-processed-in-mysq