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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With