Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL How to INSERT INTO [temp table] FROM [Stored Procedure]

This is very similar to question 653714, but for MySQL instead of SQL Server.

Basically, I have a complicated select that is the basis for several stored procedures. I would like to share the code across the stored procedures, however, I'm not sure how to do this. One way I could do this is by making the shared select a stored procedure and then calling that stored procedure from the other ones. I can't figure out how to work with the result set of the nested stored procedure. If I could put them in a temp table I could use the results effectively, but I can't figure out how to get them in a temp table. For example, this does not work:

CREATE TEMPORARY TABLE tmp EXEC nested_sp();
like image 733
Brian Fisher Avatar asked Mar 26 '09 18:03

Brian Fisher


People also ask

How do you insert the results of a stored procedure into a temporary table in mysql?

CREATE TABLE #StudentData_Log (ID INT, Name VARCHAR(100)) SELECT * FROM #StudentData_Log; Lets execute the stored procedure and insert output into above temp table. Lets check the temp table, and you can see the stored procedure output is inserted in table.

Can stored procedure insert into table?

A stored procedure is a set of SQL code specifically written for performing a task. We can write a stored procedure and execute it with a single line of SQL code. One of the tasks that you can perform with the stored procedures is to insert rows in a table.

Can you insert into a temp table SQL?

INSERT INTO SELECT statement reads data from one table and inserts it into an existing table. Such as, if we want to copy the Location table data into a temp table using the INSERT INTO SELECT statement, we have to specify the temporary table explicitly and then insert the data.

How can we store stored procedure result in temp table in SQL Server?

When the stored procedure returns a lot of columns and you do not want to manually "create" a temporary table to hold the result, I've found the easiest way is to go into the stored procedure and add an "into" clause on the last select statement and add 1=0 to the where clause.


3 Answers

The problem is, Stored Procedures don't really return output directly. They can execute select statements inside the script, but have no return value.

MySQL calls stored procedures via CALL StoredProcedureName(); And you cannot direct that output to anything, as they don't return anything (unlike a function).

MySQL Call Command

like image 102
St. John Johnson Avatar answered Sep 22 '22 00:09

St. John Johnson


You cannot "SELECT INTO" with stored procedures.

Create the temporary table first and have your stored procedure to store the query result into the created temporary table using normal "INSERT INTO". The temporary table is visible as long as you drop it or until the connection is closed.

like image 38
slaakso Avatar answered Sep 25 '22 00:09

slaakso


My first reaction was "That sounds like a view to me". Doesn't that abstract it enough so you can just add the variability into an SP per case?

Anything that adds a temp table that wouldn't otherwise be there is a very likely antipattern.

like image 24
dkretz Avatar answered Sep 25 '22 00:09

dkretz