MySQL: Get result from MySQL procedure call - mysql

Situation: Having a SQL procedure which "returns" result via "SELECT x" statements. For some reasons it is not allowed to change it to a function or changing that procedure in any way. How can I obtain the result like:
set #result = 0;
#result = call SomeProcedure(#p1, #p2);
But since it is a procedure not a function above code won't compile/work. How can I achieve that in MySQL. In C++ it works but in MySQL I found no way ...

It is not possible.
Result sets returned from select ... will always be returned to the caller of the first procedure, even if you make several levels of sub calls.
Functions return a value (but not a result set) that you can use inside other procedures or functions.
Your only option is to either set session variables or to store the result in a temporary table that the calling procedure knows about.

Related

RODBC not returning results when calling nested stored procedures in MySQL

I have a nested MySQL stored procedure that's basically structured like this:
CREATE DEFINER=`root`#`localhost` PROCEDURE `sp_main`(in param1 int, in param2 int)
BEGIN
declare refid int;
# do some stuff here
select refid;
call sp_child(param1, param2, refid);
END
Both sp_main and sp_child are stored procedures, the problem is sp_child itself has a return value (call it refid2) and when I use sqlquery in RODBC I always ended up getting refid2 instead of refid (which is what I need), although when I run it in MySQL Workbench I ended up seeing both refid and refid2 when call sp_main(...). All sp_child param are "in" params and the only "out" value is returned in the end of sp_child call.
Is there any way I could either:
Change the code in sp_main so that the output in sp_child is does not gets shown? I tried to use set #output = call sp_child(...); to suppress the output showing up when calling sp_main but that ended up giving me a syntax error. The sp_child unfortunately can not be modified as it is shared with several other SPs and changing the params would require changing all of the dependent SPs. Also obviously making sp_child to become a function is also out of question.
OR
Somehow use sqlquery in RODBC (or another R package or function) to somehow get both the output from the sp_child call AND sp_main call?
Thanks!

Call Scalar Function From HANA Procedure

​I have a function that is defined as:
​Where XXXXX is my schema YYYYY is my package.
PROCEDURE "XXXXX"."YYYYY.SPATIAL::SP_GA_PT_PATH_DISTANCE" (IN PID NVarChar(36))
LANGUAGE SQLScript
SQL SECURITY INVOKER
--DEFAULT SCHEMA <default_schema_name>
AS
BEGIN
I want to call a function and assign the result to a variable, I have tried the following two ways:
intIntersect := XXXXX.YYYYY.SPATIAL::GA_INTERSECT (32.925148, -117.020051,
32.924672, -117.019454,
32.924488, -117.020322,
32.924849, -117.019759);
​SELECT XXXXX.YYYYY.SPATIAL::GA_INTERSECT (32.925148, -117.020051,
32.924672, -117.019454,
32.924488, -117.020322,
32.924849, -117.019759) INTO intIntersect FROM DUMMY;
​I have played with different permutations of this, but nothing works.
Any ideas?
Thanks.
What you describe as a FUNCTION is really a PROCEDURE in your code example.
These differ in the ways you can call either of them.
Procedures need to be called via the CALL statement.
Functions can either be used as scalar function in all places where you can use expressions (i.e. the projection list of a SELECT-statement) or, for table-typed functions, like a table in the WHERE condition.
The parameters handed over to the procedure seem to be a list of data items.
The general way to pass "lists" of parameters is to use a table-type parameter:
CREATE FUNCTION "XXXXX"."YYYYY.SPATIAL::SP_GA_PT_PATH_DISTANCE"
(IN_PIDS TABLE (PID NVARCHAR(36)) )
RETURNS TABLE (DISTANCES DECIMAL)
AS
BEGIN
SELECT * FROM :IN_PIDS;
...

Call a stored procedure from another stored procedure using the returned value from a SELECT statement as an argument for said stored procedure?

I am using the 'Procedures' GUI in phpMyAdmin and I have a SP that has one IN param which it uses to return some info, part of which is used to call a second SP. I currently have it set up so that when the first SP has returned I use the ID to call a second SP. So I end up with multiple DB calls when I only want one and for the DB to do the heavy lifting.
The second SP is on another DB as well, both DB's are on the same server.
I want to return the results from both the SP and the initial SELECT statement.
I thought that this would work:
SELECT result1, #id = result2, result3, result4 FROM somewhere WHERE this = that
CALL DB2.second_sp(#id)

MySQL command line: Don't display results of query in a stored procedure

I've got a procedure that uses a loop with a SELECT statement, but the statement is actually just to set a variable. That means there's a lot of stuff being displayed that I don't need to see, and it's flooding my terminal.
Here's an example of what I mean, though this isn't actually what I'm running (because that's company information):
DROP PROCEDURE IF EXISTS test;
DELIMITER #
CREATE PROCEDURE test()
BEGIN
SET #key:=1;
testloop: REPEAT
SELECT
#dummyString := stringField
FROM
aTable;
SET #dummyStringAll :=CONCAT(#dummyStringAll,$dummyString);
SET #key := #key + 1;
UNTIL #key>10
END REPEAT testloop;
END #
DELIMITER ;
Is it possible to run SELECT (whether inside a procedure or not) and not show the results from a SELECT query? Maybe not the most important thing in the world, but it would be helpful.
Stored procedures will return a query resultset if it isn't stored in a variable.
How does it know that you are storing the result in a variable?
Not be using variables in the query but by using the SELECT value INTO <variable> syntax in the query. see: 13.2.9.1 SELECT ... INTO Syntax
From the FAQ:
1) Can MySQL 5.6 stored routines return result sets?
Stored procedures can, but stored functions cannot. If you perform an ordinary SELECT inside a stored procedure, the result set is returned directly to the client.
So, using the 'SELECT ... INTO ...' syntax will prevent the procedure returning the resultset from a query.

Return array in stored function

I am trying to use the result of a stored function in a WHERE statement in MySQL (5.x), but it fails because in the function I am selecting values from a table into an INT variable and then returning them/it, which obviously doesn't work if the SELECT returns more than 1 row. I've tried returning a TABLE (as I understood TABLE means array in MySQL) but that didn't work either.
Is there any way that I could do something like:
SELECT ID FROM myTable WHERE ID IN my_function(params);
Thank you.
This cannot be done...
First, you cannot use a stored function to return multiple results - you would need to use a stored procedure.
The MySQL docs state:
Statements that return a result set can be used within a stored procedure but not within a stored function.
Second, you cannot use a stored procedure in a query - see this SO question.
Have you considered using 'HAVING ...' at the end of your query?