How to join the result set of two stored procedures - mysql

I have a stored procedure which contains more that 25 queries and pass the line limit of MySQL stored procedure causing memory exhausted error.
So what I want is to divide it into two stored procedures and create a third stored procedure to union the two procedures. Can anyone help me with this?

Just push the results of both sp calls into a temp table:
/*Create a table with the same columns that the sp returns*/
CREATE TABLE #tempblahblah(blahblahblah NVARCHAR(50))
INSERT #tempblahblah ( blahblahblah )
EXEC MyStored 0
INSERT #tempblahblah ( blahblahblah )
EXEC MyStored 1
SELECT * FROM #tempblahblah

Related

Stored Procedure Data Not being Populated in Table

CREATE TABLE TempTable (
[Fiscal Quarter] varchar(50),
[Unique ID] int,
[Forecasted Amount (converted)] Numeric (25,2),
[Lead Source] varchar(50),
[AD] varchar(50),
[Stage] varchar(50),
[Opportunity Name] varchar(50))
INSERT INTO TempTable Exec [MOPs].[MSP_INQTRWon_Final_Proc_VG]
**
Question: When I insert data from stored procedure into a table; it shows 0 rows affected. However when I execute the stored procedure it says 455 rows affected.
**
However when I execute the stored procedure it says 455 rows affected.
When you execute the stored procedure it should return rows like select statement in order to insert those rows into a table. Instead if it's just doing some operation on those rows there is nothing to insert.
Please confirm whether your stored procedure returns rows when you execute.
In MySQL stored procedure is executed via call procedurename();
https://dev.mysql.com/doc/refman/8.0/en/call.html
SQLServer use exec. Is your database MySQL? Then which version?

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);

How to insert the data coming from stored procedure result into temp table with out creating temp table

I have a stored procedure which returns 110 columns, I want to insert the data coming from that stored procedure into a temp table.
Here is one solution
select *
into #tempTable
from users -- it is working fine
BUT coming to here
EXEC Sp_Users_get 12 into #tempTable
it is not working.
Please help me for solution
INSERT INTO #TempTable
EXEC Sp_Users_get 12
GO
See this, I think there may be your answer..
Select Columns from Stored Procedure Resultset
Check this out also
How to INSERT data from Stored Procedure to Table – 2 Different Methods

Trying to use stored procedure to access three tables but receiving only one row

So far I have tried many different ways of accessing the data on three tables using a stored procedure. First I tried a simple select statement :
create procedure reportCodes () begin
SELECT Numbers.serial_numb, numOwner.lName, numOwner.fName, numOwner.email,
location.long, location.lat, Numbers.dateValidated
FROM Numbers, Owner, location
WHERE Numbers.used = true AND Numbers.id=numOwner.Numbers_id AND
Numbers.id=location.Numbers_id;
end$$
(names changed to protect the guilty)
Running the stored procedure in phpmyadmin results in the first instance of the record (1 out of two ‘true’ in the test database). Running just:
SELECT Numbers.serial_numb, numOwner.lName, numOwner.fName, numOwner.email,
location.long, location.lat, Numbers.dateValidated
FROM Numbers, Owner, location
WHERE Numbers.used = true AND Numbers.id=numOwner.Numbers_id AND
Numbers.id=location.Numbers_id;
in the phpmyadmin SQL tab returns both records. Then I tried a temp table:
create procedure reportCodes () begin
CREATE TEMPORARY TABLE used_numbers AS (
SELECT Numbers.serial_numb, numOwner.lName, numOwner.fName, numOwner.email,
location.long, location.lat, Numbers.dateValidated
FROM Numbers, Owner, location
WHERE Numbers.used = true AND Numbers.id=numOwner.Numbers_id AND
Numbers.id=location.Numbers_id);
SELECT * FROM used_numbers; end$$
Returns 1 of 2 records as the procedure but both records in console. Finally I tried changing my table to a join:
CREATE PROCEDURE reportCodes()
begin
create temporary table used_numbers AS (
SELECT Numbers.serial_numb, numOwner.lName, numOwner.fName, numOwner.email,
location.long, location.lat, Numbers.dateValidated
FROM Numbers JOIN numOwner
ON Numbers.id=numOwner.Numbers_id
JOIN location ON
numOwner.Numbers_id=location.Numbers_id
WHERE Numbers.used = true
);
SELECT * FROM used_numbers; end$$
Same results as above. I’m at a loss as to why running just the SQL would show both test records but running the procedure with the exact same code only yields one.
Thanks
in your query, numOwners isn't a valid table being selected against, so something's wrong. Have you tried running your SQL in the Query window in phpMyAdmin to ensure that the EXACT same query is returning 2 rows?
I presume the "Owner" table is supposed to be "numOwner", so I've re-written the stored procedure call below. Also, I'm not sure what types of values you're storing in Numbers.used to evaluate to "TRUE". I will presume you're using a TINYINT(1), so I've altered that, as well. I hope this helps.
DELIMITER $$
USE `db`$$
DROP PROCEDURE IF EXISTS `reportCodes`$$
CREATE PROCEDURE `reportCodes`()
BEGIN
SELECT
n.serial_numb,
o.lName,
o.fName,
o.email,
l.long,
l.lat,
n.dateValidated
FROM Numbers n
INNER JOIN numOwner o ON n.id=o.Numbers_id
INNER JOIN location l ON n.id=l.Numbers_id;
WHERE n.used = 1
END$$
DELIMITER ;

Querying on the stored procedure result set

Im using SQL Server 2008. I have a result set from the stored procedure and i want to fire some more queries on the result set for e.g order by desc /asc and some querying. So what is the best way to do it. Using views or by using OPENQUERY.
Please help.
You can save results of stored procedure calls in any table or table variable that has same number and type of fields as the result set returned by the stored procedure, for example:
CREATE TABLE #temp(col1 INT, col2 VARCHAR(10))
INSERT INTO #temp(col1, col2)
EXEC usp_Proc1(#param1)
SELECT *
FROM #temp
Condition is that usp_Proc1 returns rows consisting of columns of type INT and VARCHAR(10) (in that order).