简体   繁体   中英

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.

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.

这不是一个好的做法,但您可以使用触发器来执行您的程序。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM