Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL Calling One procedure in another and Inserting values into temporary table

I have two procedures say proc1 and proc2. I am calling one mysql procedure within another.

In proc1 I want to insert values from proc2 into proc1 temporary table.The proc2 returns two columns but i want to insert only one column into proc1 temporary table.

The Output of Proc2 is as Below

Hrs(Timestamp)    Status
09:30             IN,OUT,IN,OUT 
04:30             IN,OUT
07:30             IN,OUT,IN,OUT
04:25             IN,OUT

Proc1 Code

CREATE PROCEDURE Proc1()
BEGIN
   DROP TABLE IF EXISTS TempWorkedHrs ;
   CREATE TEMPORARY TABLE TempWorkedHrs(WorkedHrs TIMESTAMP); 

   INSERT INTO TempWorkedHrs(WorkedHrs)
   CALL Proc2(); 

   SELECT SUM(WorkedHrs) INTO @TotalHrs
     FROM TempWorkedHrs;        
END //

The second column in proc2 is of no importance to me when I insert values into temporary table in proc1.

Q1. How to insert a particular column returned from procedure into temporary table? In my case first column from proc2.

like image 321
ArrayOutOfBound Avatar asked Jun 25 '13 05:06

ArrayOutOfBound


1 Answers

Short answer: This is not possible.

Long answer: This is not possible, but there are workarounds to achieve the same effect.

Option 1: Add a dummy column to your temporary table. Insert all columns from within Proc2 into your temporary table. Then drop the dummy column. Dirty.

Option 2: Add a parameter to Proc2, a BOOLEAN is probably a good choice. Insert more or less columns depending on the parameter value. Less dirty.

Option 3: Do you really need Proc2 be a procedure? In other words, does it really modify the data (or more generally, the environment) before selecting data? In other words, wouldn't a view be more suitable in this case?

[edit]

Thanks to James Holderness' pointer, I realized that you may not be aware that a stored procedure has no return value (there is no way around it). I took it for granted that by "Proc2 returns data", you actually meant "Proc2 writes into TempWorkedHrs some data that Proc1 will read back". If you can, please avoid this approach (either option 1. or 2. is dirty anyways). You most probably need a view here.

like image 56
RandomSeed Avatar answered Nov 06 '22 11:11

RandomSeed