I have three tables and I want to sum all the result columns of each table and return these values from the function I define to do all the process. Is it possible to return those three float summation from a single mysql function ?
My dirty solution is: 1. Concatenating values in a string. 2 returns string. 3 Splits returned string to values. I assume that it is not elegant and I sure this have limitations but it works for simple cases
Also is necessary create the splitting function because Mysql has not this function:
First edit your function.
CREATE FUNCTION yourFunctionWith2valuesForReturning()
BEGIN
DECLARE var1 VARCHAR(255);
DECLARE var2 VARCHAR(255);
// Your function logic here. Assign values to var1 and var2
RETURN CONCAT_WS('|', var1, var2);
END
Now you can split your returned value calling this function:
CREATE FUNCTION SPLIT_STR(x VARCHAR(510), delim VARCHAR(12), pos INT) RETURNS VARCHAR(255) DETERMINISTIC
BEGIN
RETURN REPLACE(SUBSTRING(SUBSTRING_INDEX(x, delim, pos),
LENGTH(SUBSTRING_INDEX(x, delim, pos -1)) + 1),
delim, '');
END
For getting the values
SET value1 = SPLIT_STR(returnedVAlue, '|', 1);
SET value2 = SPLIT_STR(returnedVAlue, '|', 2);
Notes:
You can use a string as delimiter, f.e: '|sep|' or another you want.
SPLIT_STR function is cortesy of http://blog.fedecarg.com/2009/02/22/mysql-split-string-function/
About splitting a string in mysql see also this question: Split Varchar into Character in MySQL
The correct way to do this would be using a stored procedure:
CREATE PROCEDURE stored_proc(IN arg1 INT, IN arg2 INT, OUT retVal1 FLOAT, OUT retVal2 FLOAT, OUT retVal3 FLOAT)
You can then assign the variables with
SELECT x*y INTO retVal1;
...
and call the stored procedure with @variables to access them:
CALL stored_proc(1, 2, @retVal1, @retVal2, @retVal3);
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