Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL: How to insert the result of stored procedure into temporary table

I want to insert the results of a stored procedure into a temporary table, like this:

CREATE temporary TABLE NEWBalance (VendorAmount NUMERIC(15,2),   
                                   UserBalanceAmount NUMERIC(15,2));  

INSERT NEWBalance call SP VenAccNo,PeopleId;

But that produces an error:

Error Code: 1064. You have an error in your SQL syntax; check 
the manual that corresponds to your MySQL server version for 
the right syntax to use near 'call SP VenAccNo,PeopleId' at line 1

Is there a way to do this?

like image 654
SHIVA73 Avatar asked Aug 06 '13 05:08

SHIVA73


People also ask

Can we return temporary table in stored procedure?

Stored procedures can reference temporary tables that are created during the current session. Within a stored procedure, you cannot create a temporary table, drop it, and then create a new temporary table with the same name.

Can we access a temp table of one stored procedure from another stored procedure?

Though you can access a temporary table inside another connection when creating a stored procedure, you cannot access a temporary table when “executing” a stored procedure inside another connection.


1 Answers

Unfortunately you still can't do that in MySql.

A possible solution is to modify your SP and make it do INSERT into a temporary table.

CREATE PROCEDURE your_sp(...)
BEGIN
    -- do your processing
    ...
    -- insert results into a temporary table
    INSERT INTO NEWBalance ...
    SELECT ...;
END

Then your flow is like this

CREATE temporary TABLE NEWBalance 
(
 VendorAmount NUMERIC(15,2),
 UserBalanceAmount NUMERIC(15,2)
);

CALL your_sp (...);

-- do your processing on data in a temporary table
...

DROP TEMPORARY TABLE NEWBalance;
like image 94
peterm Avatar answered Nov 07 '22 21:11

peterm