I am calling a stored procedure in a loop in another stored procedure, But Inner SP runs only the first time. No error is shown there, I can't seem to find the reason, Any help would be highly appreciated
DELIMITER $$
USE `xxx_db`$$
DROP PROCEDURE IF EXISTS `wrapper`$$
CREATE DEFINER=`root`#`%` PROCEDURE `wrapper`(p_StartPeriod DATE, p_EndPeriod DATE)
BEGIN
DECLARE v_startperiod DATE;
DECLARE v_endperiod DATE;
DECLARE v_nextdate DATE;
SET v_startperiod = p_StartPeriod;
SET v_endperiod = p_EndPeriod;
SET #TempStart = v_startperiod;
WHILE (#TempStart < v_endperiod) DO
SET v_nextdate = DATE_ADD(#TempStart, INTERVAL 1 DAY);
call innersp(#TempStart,v_nextdate);
SET #TempStart = DATE_ADD(#TempStart, INTERVAL 1 DAY);
END WHILE;
END$$
DELIMITER ;
In this code, The date arguments are passed to the inner SP, if a date range is given, Inner SP runs only for the first date coming in loop. Please help me to find the issue. The inner SP works fine when it is called alone, It inserts data into a table, but since the number of rows are huge I want to insert on a per day basis, instead of doing the complete date range. It's arguments are dates. That is why I wrote a wrapper SP to call this inner SP giving arguments per day for the complete date range.
I have created a database called stock trades and two tables (company data and stock_data) I want to create a procedure that will help me find stocks over 500K volume and in the technology sector. sector variable is in table company data and volume variable in stock_data) here is my code so far:
stockcode
create procedure highvolumetechstock
as
select Volume
from stock_data
INNER JOIN Companydata ON stock_data.Volume = Companydata.Sector
where Sector = 'Technology' and Volume > 500000
exec highvolumetechstock
Here is how to declare in procedure in mysql. Please note that, as commented, using a procedure is does not make a lot of sense, as you could obtain the same result with a simple SQL query.
delimiter //
CREATE PROCEDURE highvolumetechstock()
BEGIN
SELECT Volume
FROM stock_data
INNER JOIN Companydata ON stock_data.Volume = Companydata.Sector
WHERE Sector = 'Technology' AND Volume > 500000
END //
delimiter ;
Then you execute the procedure with :
CALL highvolumetechstock();
I am creating a very simple store procedure with a query, but when i use the store procedure IN parameter in the query it gets stuck and does not execute the query, but if i put the value direct to the query it works.
This works:
CREATE PROCEDURE `cap-reports`.ffap_test()
BEGIN
select * FROM students WHERE name='Fernando';
END
This does not, i spent 10 minutes and it never returned
CREATE PROCEDURE `cap-reports`.ffap_test(IN pName VARCHAR(10))
BEGIN
select * FROM students WHERE name=pName;
END
call `cap-reports`.ffap_test('Fernando');
What mistake i am doing here? I never had this problem before
This procedure works for me. Maybe it's the difference in database of the procedure and the students table? Or a missing semi-colon?
CREATE PROCEDURE `cap-reports`.ffap_test(IN pName VARCHAR(10))
BEGIN
select * FROM `cap-reports`.members m WHERE m.Username = pName;
END
;
CALL `cap-reports`.ffap_test('winkbrace');
I have this Stored Procedure which is not returning any records while the SELECT statement in it is working just fine. Running the SP like this:
CALL get_due_amount('654321');
returns nothing. No errors are shown.
What is wrong with this Stored Procedure? Txs.
DELIMITER //
CREATE DEFINER=`root`#`localhost` PROCEDURE `get_due_amount`(
IN `_booking_ref_no` VARCHAR(50))
NO SQL
SELECT
ph.booking_id,
Cast(SUM(ph.amount) AS DECIMAL(8,2)) as paid,
b.booking_total,
b.booking_ref_no,
(b.booking_total - SUM(ph.amount)) as due
FROM `i_payments_history` ph JOIN
pt_bookings b on b.booking_id = ph.booking_id
WHERE b.booking_ref_no LIKE '_booking_ref_no'
GROUP BY b.booking_id;
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 ;