Nested SP to return table data - mysql

I want to return some ids from stored procedure, which would be used in "in Clause-Mysql".
Ex.
create procedure getid
as
begin
select empid from table
end;
create procedure getdata
As
begin
select * from employees where empid in (call getid(3))
end;
the above sample is simple scenario, but i want to implement a complicated query ,so i cannot join in getdata storedprocedure. simply i want to use nested sp which returns table of data should be used in in clause.

Although a mysql procedure can return a resultset (not a table), you cannot use it as a table in another query or stored procedure. Mysql functions cannot return table either.
You either have to use a view instead of a stored procedure or the stored procedures must use the same temporary table to pass data to each other.

Related

How to write a procedure in Mysql with table name passed as parameter

I want to write a MySQL procedure. It should take a parameter, which is table name.
I have two tables with similar schema and I want to perform similar operations on them. I should run a select query which is a self join and then load results into cursor and do something based on that.
CREATE PROCEDURE MY_PROCEDURE (table_name CHAR(15)
BEGIN
//
DECLARE A CURSOR HERE, WRITE AN INNER JOIN QUERY AND LOAD results into cursor
//
.......
.......
.......
END
Now I need to call the Procedure
CALL MY_PROCEDURE('First_table');
CALL MY_PROCEDURE('Second_table');
My goal is to re-use code here.

MySQL Stored Procedures: Using a result set returned from a stored procedure in another stored procedure with MYSQL 5.6

In our MySql database there are 3 stored procedures Sp1(), Sp2() and Sp3() . There is logic in Sp3() which I want to use in the stored procedures Sp1() and Sp2(), I know that using a temporary table in Sp3() and using that temporary table in Sp1() and Sp2() works.
To save time and memory it is preferred not to create a temporary table.
One extra piece of info is I can return a result set from a stored procedure to my client. But I am unable to make this store procedure (SP3) work like a subquery, where the result set is returned from the Stored procedure Sp3() and can be compared with another table in Sp2() and Sp1() based on a id key.
I want to try something like below, which can be used in stored procedures Sp1() and Sp2():
Assuming Sp3() returns an id field along with other fields:
Select
id
from
EmployeeTable e
where 5000 >
(Select salary from (call Sp3()) where id= e.id);

Apply mysql stored procedure on a table column

I have a mysql stored procedure which accepts a variable and returns a result (the result is produced by a query):
mysp (var1) => col1,col2
I want to feed the variable from a table to produce an enhancement query such :
table1 => colA,colB,var1
select colA,colB, mysp (var1) from table1
is that possible ?
IMHO possible options are:
Move select to your stored procedure if possible and return final result set;
From your SP mysp create and populate temp table. Call procedure. Then join temp table in your select. Note: Temp tables have session scope.

Mysql stored procedure called successfully but no output records shown

I have used the following code to create a procedure:
DELIMITER //
CREATE procedure GetBooksbyBorrowerID (IN Bor_id VARCHAR(10))
BEGIN
SELECT borrower_details.Borrower_ID ,borrower_details.Book_ID, book_mst.book_Title,book_mst.LANGUAGE, borrower_details.borrowed_from_date
FROM borrower_details
JOIN book_mst
ON borrower_details.BOOK_ID = book_mst.ISBN
WHERE (borrower_details.borrower_id = 'Bor_id');
END //
When I call this procedure, it says Mysql query executed successfully, but does not show the output records. And there are records in the database that match the criteria in the query. I use the following statement to call:
CALL GetBooksbyBorrowerID ('BOR001');
What should I do to view the output records?
You are comparing borrower_details.borrower_id with the string 'Bor_id' and not the parameter.
Use WHERE (borrower_details.borrower_id = Bor_id);

MySQL: count number of rows returned by a procedure

I want to Insert the records returned by procedure in to temporary table in another procedure
I have a Procedure Like Below
call getName()
Now I want to Check whether the above procedure is returning records or not.
I want to do this in another procedure. So I am trying to insert the records returned from getName() procedure in to temp table in another procedure to carry out count operation.
Let me know how to count no of rows returned by one procedure in another procedure.
Could you let me know how to insert values into temp table from stored procedure in Mysql
create temporary table <name> select * from <some results>
Let me know how to count no of rows returned by one procedure in another procedure.
select count(*) from <name>