I am very green when it comes to MySQL Stored Procedures and i have been having trouble with selecting all payments received in the current month and looping through them to see which accounts did not pay the monthly fee, my problem seems to be with the LIKE statement in the declaration of the cursor. Here is the code:
CREATE DEFINER=`wisper`@`%` PROCEDURE `process_rejections`()
BEGIN
DECLARE cursor_ID INT(11);
DECLARE account_id INT(11);
DECLARE amount_paid DECIMAL(10,2);
DECLARE date_paid DATETIME;
DECLARE cur_year INT DEFAULT (SELECT YEAR(CURRENT_DATE()));
DECLARE cur_month INT DEFAULT (SELECT MONTH(CURRENT_DATE()));
DECLARE comp_date VARCHAR(10) DEFAULT (SELECT CONCAT(cur_year,'-',cur_month));
DECLARE done INT DEFAULT FALSE;
DECLARE cursor_i CURSOR FOR
SELECT `id`, `account_id`, `amount_paid`, `date`
FROM `payments_received`
WHERE `date`LIKE CONCAT('%',@comp_date,'%');
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN cursor_i;
read_loop: LOOP
FETCH cursor_i INTO cursor_ID, account_id, amount_paid, date_paid;
IF done THEN
LEAVE read_loop;
END IF;
//Do Stuff here like change the status of the account
END LOOP;
CLOSE cursor_i;
END
IF i remove this:
WHERE `date` LIKE CONCAT('%',@comp_date,'%');
Everything works but it obviously selects * payments received instead of those that occured during ,for example, Aug 2017. I do not want to select * because over time there will be hundreds of thousands of rows in the specific table and i do not want the overhead to become too much. I have also tested this:
WHERE `date` LIKE '2017-08-11';
Which also does not work. During debuggin i also tried to use a specifi date like this:
WHERE `date` = '2017-08-11';
And everything worked nicely but it is obviously just to test and see if the WHERE cluase was syntactically correct inside the cursor declaration.
I am at a lost here and would appreciate some assistance from you guy's.
Thank You in advance.