THE CURSOR IS NOT GOING TO THE ELSE STATEMENT. WHAT AM I DOING WRONG? HOW CAN I DEBUG IN MYSQL ?
I am trying to get the list of task Ids with null values and then for each taskID, I am calling a SP to create start data and due date of the missing tasks(greater than the max(start_date available) until the current_date
DROP PROCEDURE IF EXISTS zcursor_ADDTA1;
DELIMITER ;;
CREATE PROCEDURE zcursor_ADDTA1(
#out retCount int
)
BEGIN
#DECLARE C_ID INT;
DECLARE C_TASK_ID INT;
DECLARE C_pattern varchar(100);
DECLARE C_REM INT;
DECLARE ACT_T_STTM VARCHAR(100);
DECLARE ACT_T_DTTM VARCHAR(100);
DECLARE TZ VARCHAR(100);
DECLARE T_STDT DATE;
DECLARE T_EXDT DATE;
DECLARE retCount INT;
DECLARE done INT DEFAULT FALSE;
DECLARE TA_MAX_ST_DT DATE;
#DECLARE P1 INT;
DECLARE cursor_i CURSOR FOR SELECT TASK_ID, PATTERN, reminder_days_before, ACTUAL_TASK_START_TIME,ACTUAL_TASK_DUE_TIME,TIMEZONE,
START_DT,EXP_DT FROM OCC_ML_TASK_RECURRENCE TR WHERE PATTERN = 'DAILY' AND CALENDAR_TYPE='' AND IS_CANCELED =0 AND EXP_DT>CURRENT_DATE();
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
SET retCount = 0;
OPEN cursor_i;
read_loop: LOOP
FETCH NEXT FROM cursor_i INTO C_TASK_ID,C_pattern,C_REM,ACT_T_STTM,ACT_T_DTTM,
TZ,T_STDT,T_EXDT;
IF done THEN
LEAVE read_loop;
ELSE
SELECT MAX(TA.START_DTM)FROM
OCC_ML_TASK_ACTIVITY TA
WHERE TA.TASK_ID=C_TASK_ID INTO TA_MAX_ST_DT;
SELECT DATE_ADD(TA_MAX_ST_DT,INTERVAL 1 DAY) INTO TA_MAX_ST_DT;
WHILE (TA_MAX_ST_DT<=CURRENT_DATE())
DO
BEGIN
IF WEEKDAY(TA_MAX_ST_DT)=4 THEN
CALL ZTA_MISSINGDATA_INSERT(C_TASK_ID ,TA_MAX_ST_DT);
SELECT DATE_ADD(TA_MAX_ST_DT,INTERVAL 3 DAY)INTO TA_MAX_ST_DT;
ELSEIF WEEKDAY(TA_MAX_ST_DT)=5 THEN
#"NO INSERT "
SELECT DATE_ADD(TA_MAX_ST_DT,INTERVAL 2 DAY)INTO TA_MAX_ST_DT;
ELSEIF WEEKDAY(TA_MAX_ST_DT)=6 THEN
#"NO INSERT "
SELECT DATE_ADD(TA_MAX_ST_DT,INTERVAL 1 DAY)INTO TA_MAX_ST_DT;
ELSE
CALL ZTA_MISSINGDATA_INSERT(C_TASK_ID ,TA_MAX_ST_DT);
SELECT DATE_ADD(TA_MAX_ST_DT,INTERVAL 1 DAY) INTO TA_MAX_ST_DT;
END IF;
END;
END WHILE;
END IF;
END LOOP;
CLOSE cursor_i;
END;
;;
Related
Below procedure code running with out error . Line select FOUND_ROWS() returning n no. rows but cursor curs did not loop all rows
CREATE DEFINER=`root`#`%` PROCEDURE `middleLocationAutoUpdatePorc`()
BEGIN
declare vname varchar(45);
declare vmobile varchar(20);
declare vsapid varchar(6);
declare vuser varchar(45);
declare vday date;
declare vmintime time;
declare vmaxtime time;
declare vminLocation mediumtext;
declare vmaxLocation mediumtext;
declare vmiddlelocation mediumtext;
declare vdistance int(11);
declare vdrdid int(11);
declare vn_no_details int(11);
declare uatt varchar(45) default 'X';
declare adatta varchar(45) default 'X';
declare b_not_found BOOL DEFAULT FALSE;
declare curs CURSOR FOR
SELECT user ,day, mintime,maxtime_mobile,minLocation,maxLocation,distance,n_no_details,drdid FROM mydb.statusreport where day >= '2017-06-26';
DECLARE CONTINUE HANDLER FOR NOT FOUND SET b_not_found = true;
OPEN curs;
loop1: LOOP
FETCH curs INTO vuser,vday,vmintime,vmaxtime,vminLocation,vmaxLocation,vdistance,vn_no_details,vdrdid;
IF b_not_found THEN
LEAVE loop1;
END IF;
-- select FOUND_ROWS();
case substring_index(vuser,"-",-1)
when 'RSM' then
select username,Name,mobile into vsapid,vname,vmobile from mydb.supervisor where idSu=CONVERT(substring_index(vuser,"-",1),UNSIGNED INTEGER);
when 'ASM' then
select userName,name,mobile into vsapid,vname,vmobile from mydb.executive where exeid=CONVERT(substring_index(vuser,"-",1),UNSIGNED INTEGER);
when 'F' then
select sapId,name,phone into vsapid,vname,vmobile from mydb.user where idUser=CONVERT(substring_index(vuser,"-",1),UNSIGNED INTEGER);
end case;
select userGiven , adminGiven into uatt ,adatta from userdaystatus st where st.date =vday and st.idUser=CONVERT(substring_index(vuser,"-",1),UNSIGNED INTEGER) and st.userType=substring_index(vuser,"-",-1);
set vmiddlelocation=( select location from mydb.dailyReportDetails as dtb where dtb.idDailyReport=vdrdid and dtb.counter>=(abs(vn_no_details/2) ) order by dtb.idDailyReportDetails asc limit 1);
call mydb.addOMiddleLocation(day(vday), month(vday),
year(vday),vuser, vname,
vsapid, vmobile, vmintime,
vmaxtime, SUBTIME(vmaxtime,vmintime),
vminLocation,
vmiddlelocation,
vmaxLocation,Round(vdistance/1000,2),
uatt, adatta);
END LOOP;
CLOSE curs;
END
The Procedure call mydb.addOMiddleLocation() just inserting Row on another table.There does not have any data type validation .
So what can be the problem ?
It happen for log wait time for a particular Query line
set vmiddlelocation=( select location from mydb.dailyReportDetails as dtb where dtb.idDailyReport=vdrdid and dtb.counter>=(abs(vn_no_details/2) ) order by dtb.idDailyReportDetails asc limit 1);
This is the first time I am working on Mysql stored procedure I know it is a lame question please spare me for this,
is it not possible to print any value after the END LOOP statement in MySQL procedure. If it is, how we can achieve this.
what I did for this:
BEGIN
DECLARE U_movingCity varchar(50);
DECLARE U_state varchar(50);
DECLARE U_education varchar(50);
DECLARE id2 int(10);
DECLARE RankPoint int(10) DEFAULT 0;
DECLARE movingCity2 varchar(50);
DECLARE state2 varchar(50);
DECLARE education2 varchar(50);
DECLARE cur1 CURSOR FOR SELECT id, state , education FROM user WHERE id != userid AND Enabled='y' AND Active='y';
SELECT state , education into U_state , U_education FROM user WHERE id = userid ;
OPEN cur1;
read_loop: LOOP
SET RankPoint := 0;
FETCH cur1 INTO id2, state2 , education2 ;
IF ((state2 = U_state)) THEN
SET RankPoint := RankPoint + 14;
END IF;
IF ((education2 = U_education)) THEN
SET RankPoint := RankPoint + 16;
END IF;
//this displays
select RankPoint;
END LOOP;
//this doesn't.
select id, RankPoint from user;
CLOSE cur1;
END
You should use id2 instead of id in your last query.
select `id2`, `RankPoint` from `user`;
I have a stored procedure like this :
DROP PROCEDURE IF EXISTS storedprocedure;
CREATE DEFINER = 'userid'#'%' PROCEDURE storedprocedure
(IN id int)
BEGIN
DECLARE cor int;
DECLARE sup int;
DECLARE faktur varchar(20);
DECLARE ptgs int;
DECLARE suppli varchar(50);
DECLARE bag int;
DECLARE brg int;
DECLARE qty int;
DECLARE beli int;
DECLARE net int;
DECLARE oldbeli int;
DECLARE oldnet int;
DECLARE updhrg int;
DECLARE ket varchar(100);
DECLARE idsisa int;
DECLARE qtyterima int;
DECLARE qtysisa int;
DECLARE no_more_rows BOOLEAN;
DECLARE loop_cntr INT DEFAULT 0;
DECLARE num_rows INT DEFAULT 0;
DECLARE hitung CURSOR FOR
SELECT
aptlog_orderterima.corp,
aptlog_orderterima.idsupplier,
aptlog_orderterima.faktur,
aptlog_orderterima.idpetugas,
aptlog_supplier.supplier,
aptlog_orderterima.idbagian,
aptlog_orderterimadetail.idbarang,
aptlog_orderterimadetail.beli,
aptlog_orderterimadetail.net,
aptlog_orderterimadetail.qty,
aptlog_barang.beli,
aptlog_barang.net,
aptlog_barang.updatehrg,
CONCAT(aptlog_orderterima.faktur,' From : ',aptlog_supplier.supplier)
FROM
aptlog_orderterima
RIGHT OUTER JOIN aptlog_orderterimadetail ON (aptlog_orderterima.id = aptlog_orderterimadetail.idterima)
LEFT OUTER JOIN aptlog_supplier ON (aptlog_orderterima.idsupplier = aptlog_supplier.id)
LEFT OUTER JOIN aptlog_barang ON (aptlog_orderterimadetail.idbarang = aptlog_barang.id)
WHERE
aptlog_orderterima.id = id;
DECLARE sisaorder CURSOR FOR
SELECT
aptlog_orderdetail.id,
aptlog_orderdetail.qtyterima,
aptlog_orderdetail.sisa
FROM
aptlog_orderdetail
LEFT OUTER JOIN aptlog_order ON (aptlog_orderdetail.idorder = aptlog_order.id)
WHERE
aptlog_order.corp=cor AND
aptlog_order.stat=2 AND
aptlog_order.idsupplier=sup AND
CAST(aptlog_order.waktu AS DATE) >= CURDATE()-(SELECT pengadaan_param.nilai FROM pengadaan_param WHERE pengadaan_param.id=2) AND
aptlog_orderdetail.sisa<>0 AND
aptlog_orderdetail.idbarang=brg
ORDER BY
aptlog_order.waktu
LIMIT 1;
DECLARE CONTINUE HANDLER FOR NOT FOUND
SET no_more_rows = TRUE;
OPEN hitung;
SELECT FOUND_ROWS() INTO num_rows;
the_loop: LOOP
FETCH hitung INTO cor, sup, faktur, ptgs, suppli, bag, brg, beli, net, qty, oldbeli, oldnet, updhrg, ket;
IF no_more_rows THEN CLOSE hitung; LEAVE the_loop;
END IF;
SET #awal = (SELECT aptlog_stok.qty FROM aptlog_stok WHERE corp=cor AND idbarang=brg AND idbagian=bag);
IF #awal IS NOT NULL THEN
UPDATE aptlog_stok SET aptlog_stok.qty = #awal + qty
WHERE corp=cor AND idbarang=brg AND idbagian=bag;
INSERT INTO aptlog_kartustok(waktu, corp, idbagian, idbarang, qtymasuk, qtykeluar, qtysisa, idpetugas, ket)
VALUES (now(), cor, bag, brg, qty, 0, #awal + qty, ptgs, ket); ELSE
INSERT INTO aptlog_stok(corp, idbarang, idbagian, qty)
VALUES (cor, brg, bag, qty);
INSERT INTO aptlog_kartustok(waktu, corp, idbagian, idbarang, qtymasuk, qtykeluar, qtysisa, idpetugas, ket)
VALUES (now(), cor, bag, brg, qty, 0, qty, ptgs, ket);
END IF;
IF updhrg=0 THEN
UPDATE aptlog_barang SET
aptlog_barang.beli=beli,
aptlog_barang.net=net,
aptlog_barang.updatehrg=1,
aptlog_barang.ubahharga=ket,
aptlog_barang.waktuubah=now(),
aptlog_barang.idpetugasubah=ptgs
WHERE
aptlog_barang.id=brg; ELSE
IF (updhrg=1) AND (beli > oldbeli) THEN
UPDATE aptlog_barang SET
aptlog_barang.beli=beli,
aptlog_barang.belilama=oldbeli,
aptlog_barang.net=net,
aptlog_barang.netlama=oldnet,
aptlog_barang.ubahharga=ket,
aptlog_barang.waktuubah=now(),
aptlog_barang.idpetugasubah=ptgs
WHERE
aptlog_barang.id=brg; END IF;
END IF;
SET #qty=qty;
sisa_order: LOOP
OPEN sisaorder;
FETCH sisaorder INTO idsisa, qtyterima, qtysisa;
IF #qty=0 THEN CLOSE sisaorder; LEAVE sisa_order;
END IF;
IF #qty <> 0 THEN
IF qtysisa > #qty THEN
UPDATE aptlog_orderdetail SET
aptlog_orderdetail.qtyterima=qtyterima+#qty,
aptlog_orderdetail.sisa=qtysisa-#qty
WHERE
aptlog_orderdetail.id=idsisa;
SET #qty=0;
ELSE
UPDATE aptlog_orderdetail SET
aptlog_orderdetail.qtyterima=qtyterima+qtysisa,
aptlog_orderdetail.sisa=0
WHERE
aptlog_orderdetail.id=idsisa;
SET #qty = #qty-qtysisa;
END IF;
END IF;
CLOSE sisaorder;
END LOOP sisa_order;
SET loop_cntr = loop_cntr + 1;
END LOOP the_loop;
END');
My question :
The second loop work perfectly, but the first loop just work once.
But if I remove this line "CAST(aptlog_order.waktu AS DATE) >= CURDATE()-(SELECT pengadaan_param.nilai FROM pengadaan_param WHERE pengadaan_param.id=2) AND" from sisaorder all loop work perfectly...
So can anybody help me?
How to use for loop in MySQL data set ?
FOR x IN (SELECT column FROM table_name WHERE column_2 = input_val)
LOOP
sum_total := sum_total + x.column ;
END LOOP;
This is an example how I used to loop data set in oracle.
How can this be achieved in MySql ?
How about not looping at all. It's SQL after all.
SELECT SUM(`column`) total
FROM table_name
WHERE column_2 = <input_val>
Otherwise use CURSOR
Now, equivalent loop using CURSOR will look like
DELIMITER $$
CREATE PROCEDURE sp_loop(IN input_val INT)
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE sum_current, sum_total INT DEFAULT 0;
DECLARE cur1 CURSOR FOR SELECT column1 FROM table_name WHERE column2 = input_val;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN cur1;
read_loop: LOOP
FETCH cur1 INTO sum_current;
IF done THEN
LEAVE read_loop;
END IF;
SET sum_total = sum_total + sum_current;
END LOOP;
CLOSE cur1;
SELECT sum_total;
END$$
DELIMITER ;
Here is SQLFiddle
DROP PROCEDURE IF EXISTS `foo`.`usp_cursor_example`;
DELIMITER $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `foo`.`usp_cursor_example`(
IN name_in VARCHAR(255)
)
READS SQL DATA
BEGIN
DECLARE name_val VARCHAR(255);
DECLARE status_update_val VARCHAR(255);
-- Declare variables used just for cursor and loop control
DECLARE no_more_rows BOOLEAN;
DECLARE loop_cntr INT DEFAULT 0;
DECLARE num_rows INT DEFAULT 0;
-- Declare the cursor
DECLARE friends_cur CURSOR FOR
SELECT
name
, status_update
FROM foo.friend_status
WHERE name = name_in;
-- Declare 'handlers' for exceptions
DECLARE CONTINUE HANDLER FOR NOT FOUND
SET no_more_rows = TRUE;
OPEN friends_cur;
select FOUND_ROWS() into num_rows;
the_loop: LOOP
FETCH friends_cur
INTO name_val
, status_update_val;
IF no_more_rows THEN
CLOSE friends_cur;
LEAVE the_loop;
END IF;
select name_val, status_update_val;
-- count the number of times looped
SET loop_cntr = loop_cntr + 1;
END LOOP the_loop;
-- 'print' the output so we can see they are the same
select num_rows, loop_cntr;
END
DELIMITER ;
I was trying use multiple select at one procedure to assign the value of variable.
but i got error like this "Operand should contain 1 column(s)"
this is my code :
DELIMITER $$
DROP PROCEDURE IF EXISTS `bankdb`.`charge` $$
CREATE PROCEDURE `bankdb`.`charge` ()
BEGIN
DECLARE idcust_val INT;
DECLARE balance_val FLOAT;
DECLARE balance_val1 FLOAT;
DECLARE balance_val2 FLOAT;
DECLARE productCd_val VARCHAR(10);
DECLARE productType_val VARCHAR(10);
DECLARE no_more_rows INT DEFAULT 0;
DECLARE num_rows INT DEFAULT 0;
DECLARE col_cur CURSOR FOR
select a.cust_id, a.avail_balance, a.product_cd, p.product_type_cd
from account a, product p
where a.product_cd = p.product_cd;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET no_more_rows = 1;
OPEN col_cur;
select FOUND_ROWS() into num_rows;
read_loop: LOOP
IF no_more_rows THEN
CLOSE col_cur;
LEAVE read_loop;
END IF;
FETCH col_cur INTO idcust_val, balance_val, productCd_val, productType_val;
SELECT idcust_val, balance_val, productCd_val;
IF productType_val = 'ACCOUNT' THEN
IF productCd_val = 'SAV' || productCd_val = 'CD' THEN
IF balance_val < 2000 THEN
SELECT (balance_val-10) INTO balance_val;
END IF;
ELSE
SELECT (balance_val+(balance_val*0,05)) INTO balance_val;
END IF;
ELSE
SELECT (balance_val-(balance_val*0,1)) INTO balance_val;
END IF;
/*UPDATE account SET avail_balance = balance_val WHERE account_id = idcust_val;*/
END LOOP read_loop;
END $$
DELIMITER ;
please help!
Change decimal point ',' to '.'
0,05 -> 0.05
0,1 - 0.1