I have three stored procedures which gives same result set for different conditions and joining different tables.
ex
procedure_one(StartDate,EndDate);
procedure_two(StartDate,EndDate);
procedure_three(StartDate,EndDate);
I need an another stored procedure which will gives the result from above three stored procedures. simply union of above three stored procedures. I don't want the union of 3 queries instead i want union of 3 stored procedures. I googled but unable to find good solution.
I have made few attempts as follows.
first attempt
DELIMITER //
CREATE PROCEDURE `MasterProcedure` (IN StartDate DATE,IN EndDate DATE)
BEGIN
SELECT @CALL procedure_one (StartDate,EndDate) UNION
SELECT @CALL procedure_two (StartDate,EndDate) UNION
SELECT @CALL procedure_three (StartDate,EndDate);
END //
DELIMITER ;
second attempt
DELIMITER //
DECLARE var_val1 TEXT;
DECLARE var_val2 TEXT;
DECLARE var_val3 TEXT;
CREATE PROCEDURE `MasterProcedure` (IN StartDate DATE,IN EndDate DATE)
BEGIN
SELECT @CALL procedure_one (StartDate,EndDate) INTO @var_val1;
SELECT @CALL procedure_two (StartDate,EndDate) INTO @var_val2;
SELECT @CALL procedure_three (StartDate,EndDate) INTO @var_val3;
SELECT @var_val1,@var_val2,@var_val3;
END //
DELIMITER ;
Is there any workaround to achieve this. Thanks in advance.
As I think above feature is not implemented in MySQL. See here for more information.
I came up with following solution. But not sure it is the best approach to this problem.
DELIMITER //
CREATE PROCEDURE pro_1(IN param1 data_type,IN param2 data_type);
BEGIN
CREATE TEMPORARY TABLE temp1 AS SELECT * FROM table_name WHERE $where1;
COMMIT;
END //
DELIMITER ;
as above you can create next two procedures as follows.
DELIMITER //
CREATE PROCEDURE pro_2(IN param1 data_type,IN param2 data_type);
BEGIN
CREATE TEMPORARY TABLE temp2 AS SELECT * FROM table_name WHERE $where2;
COMMIT;
END //
DELIMITER ;
DELIMITER //
CREATE PROCEDURE pro_3(IN param1 data_type,IN param2 data_type);
BEGIN
CREATE TEMPORARY TABLE temp3 AS SELECT * FROM table_name WHERE $where3;
COMMIT;
END //
DELIMITER ;
So following is the master procedure which is union of above 3 procedures.
DELIMITER //
CREATE PROCEDURE master_pro(IN param1 data_type,IN param2 data_type);
BEGIN
CALL pro_1(param1,param2);
CALL pro_2(param1,param2);
CALL pro_3(param1,param2);
CREATE TEMPORARY TABLE master_temp AS (SELECT * FROM temp1) UNION ALL (SELECT * FROM temp2) UNION ALL (SELECT * FROM temp3);
COMMIT;
END //
DELIMITER ;
SO finally if you want to get any other results from master procedure you can do as follows.
DELIMITER //
CREATE PROCEDURE another_pro(IN param1 data_type,IN param2 data_type);
BEGIN
CALL master_pro(param1,param2);
SELECT columns_from_master_temp FROM master_temp WHERE where_clause;
COMMIT;
END //
DELIMITER ;
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