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);
drop procedure if exists test;
delimiter $$
create procedure `test`()
begin
DECLARE done INT DEFAULT FALSE;
DECLARE i1 VARCHAR(20);
DECLARE i2 DATE;
DECLARE i3 VARCHAR(20);
DECLARE i4 INT;
DECLARE curs1 CURSOR FOR SELECT * FROM test11;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN curs1;
FETCH curs1 INTO i1,i2,i3,i4;
if i4=22 then
insert into test1(abc) values(i1);
end if ;
CLOSE curs1;
end ;
;;
delimiter ;
above throws an error : SQL Error [1064] [42000]: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 14
create table test11(abc varchar(20), cde varchar(20), eg varchar(20), asda varchar(20));
insert into test11 values("Hello",'1989-08-09','sdads','22');
create table test1(abc varchar(20), cde int(20), eg date, asda varchar(20));
I don't what i am doing wrong. Syntax seems to be fine.
drop procedure if exists sp;
CREATE PROCEDURE sp()
BEGIN
DECLARE intoffer INT;
SELECT max(asda) INTO intoffer FROM test11;
IF (intoffer IS NULL) THEN
SET intoffer = 1;
ELSE
SET intoffer = intoffer + 1;
END IF;
INSERT INTO test1 (asda) VALUES (intoffer);
end;
This is another procedure throwing same error. After removing the if..end-if both the code seems to work fine.
You are Missing fetch from
drop procedure if exists test;
delimiter $$
create procedure `test`()
begin
DECLARE done INT DEFAULT FALSE;
DECLARE i1 VARCHAR(20);
DECLARE i2 DATE;
DECLARE i3 VARCHAR(20);
DECLARE i4 INT;
DECLARE curs1 CURSOR FOR SELECT * FROM test11;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN curs1;
insertLoop: LOOP
FETCH FROM curs1 INTO i1,i2,i3,i4;
if i4=22 then
insert into test1(abc) values(i1);
end if ;
END LOOP insertLoop;
CLOSE curs1;
end ;
;;
delimiter ;
I want to add two cursor values where vehicle_id and vehicleno should be the same and then update the table.
example:
1.value from cur2 , totkm_t
2.value from cur3 , totkm_l
I want to add these two i.e.
totkm_f = totkm_t + ktotm_l
and then
update fuelavg SET totkm=totkm_f where vid=vid_l;
I also want such addition for other store pump but my stored procedure returns null values.
DELIMITER $$
DROP PROCEDURE IF EXISTS `searchvehicle`.`fuelavg`$$
CREATE DEFINER=`root`#`localhost` PROCEDURE `fuelavg`(IN startdate DATE, IN enddate DATE)
BEGIN
DECLARE vname_c VARCHAR(40);
DECLARE vnostring_c varchar(30);
DECLARE vowner_c varchar(40);
DECLARE vid_c,vmax_c,vmin_c,vcmpny_c,vtype_c INT;
DECLARE done INT DEFAULT FALSE;
DROP TABLE IF EXISTS fuelavg;
CREATE TABLE fuelavg
(
id int not null auto_increment primary key,
vid int,
vname varchar(40),
vnostring varchar(30),
v_type int,
vowner varchar(40),
store int,
pump int,
other int,
totfuel int,
totkm int,
vehavrg float,
cmpnyavg int,
maxavg int,
minavg int
);
BLOCK1: BEGIN
DECLARE CUR1 CURSOR FOR SELECT vehicle_owner.vid,vehicle_owner.vname, vehicle_owner.vowner,vehicle_owner.vnostring,vehicle_owner.vtype,vehicle_owner.vcompnyavg,vehicle_owner.maxavg,vehicle_owner.minavg FROM vehicle_owner;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=TRUE;
OPEN CUR1;
READ_LOOP: LOOP
FETCH CUR1 INTO vid_c,vname_c,vowner_c,vnostring_c,vtype_c,vcmpny_c,vmax_c,vmin_c;
IF done THEN LEAVE READ_LOOP;
END IF;
SET autocommit=0;
INSERT INTO fuelavg(vid,vname,vnostring,vowner,v_type,cmpnyavg, maxavg,minavg)values(vid_c,vname_c,vnostring_c,vowner_c,vtype_c,vcmpny_c,vmax_c,vmin_c);
END LOOP;
CLOSE CUR1;
END BLOCK1;
BLOCK2: BEGIN
DECLARE vid_t,totkm_t,store_t,pump_t,other_t INT;
DECLARE vnostring_t VARCHAR(30);
DECLARE indate_t,outdate_t DATE DEFAULT FALSE;
DECLARE vid_l,totkm_l,store_l,pump_l,other_l INT;
DECLARE vnostring_l VARCHAR(30); `enter code here`
DECLARE vid_f,totkm_f,store_f,pump_f,other_f INT;
DECLARE vnostring_f VARCHAR(30);
DECLARE done INT DEFAULT FALSE;
DECLARE CUR2 CURSOR FOR SELECT logentry.vid,logentry.vnostring,sum(logentry.totkm),sum(logentry.other),sum(logentry.store),sum(logentry.pump) FROM logentry WHERE indate>=startdate and indate<=enddate group by vid,vnostring;
DECLARE CUR3 CURSOR FOR SELECT logentry.vid,logentry.vnostring,sum(logentry.totkm),sum(logentry.store),sum(logentry.other),sum(logentry.pump) FROM LOGENTRY WHERE INDATE<=(SELECT MAX(INDATE) FROM LOGENTRY WHERE TANKFULL=1 AND INDATE<startdate)AND tankfull=1 GROUP BY VID;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=TRUE;
OPEN CUR2;
OPEN CUR3;
READ_LOOP: LOOP
FETCH CUR2 INTO vid_t,vnostring_t,totkm_t,other_t,store_t,pump_t;
IF done THEN LEAVE READ_LOOP;
END IF;
FETCH CUR3 INTO vid_l,vnostring_l,totkm_l,other_l,store_l,pump_l;
IF done THEN LEAVE READ_LOOP;
END IF;
UPDATE fuelavg SET totkm=(totkm_t+totkm_l),other=(other_t+other_l),store=(store_t+store_l),pump=(pump_t+pump_l) WHERE vid=vid_t AND vnostring=vnostring_t;
IF done THEN LEAVE READ_LOOP;
END IF;
SET autocommit=1;
END LOOP;
CLOSE CUR2;
CLOSE CUR3;
END BLOCK2;
select * from fuelavg;
output file attached here which currently the above query that show null values.
Finally my efforts was fruitful. The following is the code where I updated the table by adding two cursor values.
DELIMITER $$
DROP PROCEDURE IF EXISTS `searchvehicle`.`fuelavg2`$$
CREATE DEFINER=`root`#`localhost` PROCEDURE `fuelavg2`(IN startdate DATE, IN enddate DATE)
BEGIN
DECLARE vname_c VARCHAR(40);
DECLARE vnostring_c varchar(30);
DECLARE vowner_c varchar(40);
DECLARE vid_c,vmax_c,vmin_c,vcmpny_c,vtype_c INT;
DECLARE done INT DEFAULT FALSE;
DROP TABLE IF EXISTS fuelavg;
CREATE TABLE fuelavg
(
id int not null auto_increment primary key,
vid int,
vname varchar(40),
vnostring varchar(30),
v_type int,
vowner varchar(40),
store int,
pump int,
other int,
totfuel int,
totkm int,
vehavrg int,
cmpnyavg int,
maxavg int,
minavg int
);
BLOCK1: BEGIN
DECLARE CUR1 CURSOR FOR SELECT vehicle_owner.vid,vehicle_owner.vname,vehicle_owner.vowner,vehicle_owner.vnostring,vehicle_owner.vtype,vehicle_owner.vcompnyavg,vehicle_owner.maxavg,vehicle_owner.minavg FROM vehicle_owner;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=TRUE;
OPEN CUR1;
READ_LOOP: LOOP
FETCH CUR1 INTO vid_c,vname_c,vowner_c,vnostring_c,vtype_c,vcmpny_c,vmax_c,vmin_c;
IF done THEN LEAVE READ_LOOP;
END IF;
SET autocommit=0;
INSERT INTO fuelavg(vid,vname,vnostring,vowner,v_type,cmpnyavg, maxavg,minavg)values(vid_c,vname_c,vnostring_c,vowner_c,vtype_c,vcmpny_c,vmax_c,vmin_c);
END LOOP;
CLOSE CUR1;
END BLOCK1;
BLOCK2: BEGIN
DECLARE vid_t,totkm_t,store_t,pump_t,other_t INT;
DECLARE vnostring_t VARCHAR(30);
DECLARE indate_t,outdate_t DATE DEFAULT FALSE;
DECLARE vid_l,totkm_l,store_l,pump_l,other_l INT;
DECLARE vnostring_l VARCHAR(30);
DECLARE vid_f,totkm_f,store_f,pump_f,other_f INT;
DECLARE vnostring_f VARCHAR(30);
DECLARE vid_a,totfuel_a INT;
DECLARE vehavrg_a INT;
DECLARE vnostring_a VARCHAR(30);
DECLARE done,done3,done2,done5 INT DEFAULT FALSE;
DECLARE CUR2 CURSOR FOR SELECT logentry.vid,logentry.vnostring,sum(logentry.totkm),sum(logentry.other),sum(logentry.store),sum(logentry.pump) FROM logentry WHERE indate>=startdate and indate<=enddate group by vid,vnostring;
DECLARE CUR4 CURSOR FOR SELECT logentry.vid,logentry.vnostring,sum(logentry.totkm),sum(logentry.other),sum(logentry.store),sum(logentry.pump) FROM logentry WHERE indate>=startdate and indate<=enddate group by vid,vnostring;
DECLARE CUR3 CURSOR FOR SELECT logentry.vid,logentry.vnostring,sum(logentry.totkm),sum(logentry.store),sum(logentry.other),sum(logentry.pump) FROM LOGENTRY WHERE INDATE<=(SELECT MAX(INDATE) FROM LOGENTRY WHERE TANKFULL=1 AND INDATE<startdate)AND tankfull=1 GROUP BY VID;
OPEN CUR2;
BLOCK5:BEGIN
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=TRUE;
OPEN CUR4;
/* update table by value of traversing the tankfull of last month */
READ_LOOP5:LOOP
FETCH CUR4 INTO vid_t,vnostring_t,totkm_t,other_t,store_t,pump_t;
IF done THEN LEAVE READ_LOOP5;
END IF;
UPDATE fuelavg SET totkm=totkm_t,other=other_t,store=store_t,pump=pump_t WHERE vid=vid_t;
IF done THEN LEAVE READ_LOOP5;
END IF;
END LOOP READ_LOOP5;
END BLOCK5;
BLOCK3:BEGIN
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done3=TRUE;
OPEN CUR3;
READ_LOOP3:LOOP
FETCH CUR3 INTO vid_l,vnostring_l,totkm_l,other_l,store_l,pump_l;
IF done3 THEN LEAVE READ_LOOP3;
END IF;
/**********************/
/* update table by value of traversing the tankfull of last month */
BLOCK4:BEGIN
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done2=TRUE;
READ_LOOP2:LOOP
FETCH CUR2 INTO vid_t,vnostring_t,totkm_t,other_t,store_t,pump_t;
IF done3 THEN LEAVE READ_LOOP2;
END IF;
UPDATE fuelavg SET totkm=totkm_t+totkm_l,other=other_t+other_l,store=store_t+store_l,pump=pump_t+pump_l WHERE vid=vid_t AND vid=vid_l ;
IF done2 THEN LEAVE READ_LOOP2;
END IF;
END LOOP READ_LOOP2;
END BLOCK4;
/**********************/
END LOOP READ_LOOP3;
END BLOCK3;
/* calculate totfuel,averge*/
BLOCK5:BEGIN
DECLARE CUR5 CURSOR FOR SELECT fuelavg.vid,fuelavg.vnostring,(SUM(fuelavg.other) + SUM(fuelavg.pump) + SUM(fuelavg.store)) AS 'tot_fuel',(SUM(fuelavg.totKM)/ (SUM(fuelavg.other)+SUM(fuelavg.pump)+SUM(fuelavg.store))) AS 'veh_avrg' FROM fuelavg group by vid,vnostring;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done5=TRUE;
OPEN CUR5;
READ_LOOP6:LOOP
FETCH CUR5 INTO vid_a,vnostring_a,totfuel_a,vehavrg_a;
IF done5 THEN LEAVE READ_LOOP6;
END IF;
UPDATE fuelavg SET totfuel=totfuel_a,vehavrg=vehavrg_a WHERE vid=vid_a AND vnostring=vnostring_a;
IF done5 THEN LEAVE READ_LOOP6;
END IF;
END LOOP READ_LOOP6;
CLOSE CUR5;
END BLOCK5;
/**********************/
CLOSE CUR3;
CLOSE CUR2;
CLOSE CUR4;
END BLOCK2;
select * from fuelavg;
END$$
DELIMITER ;`
I'm not an expert with MySQL and I've problems with this Stored Procedure.
I'm trying to do the SP with conditions but I don't know what is wrong here, I have a mistake:
Error Code: 1064. You have an error in your SQL syntax; check the
manual that corresponds to your MySQL server version for the right
syntax to use near 'declare done int default 0; declare continue
handler for sqlstate '02000' set' at line 16
delimiter $$
create procedure getListPrsn(IN idEquipo INT, IN tipo char, IN Puesto INT)
begin
declare varJefe int;
declare eqpSupJefe int;
declare jefeONo cursor for select tblpuesto.PtoLiderEqp from tblequipo
inner join tblpuesto on (tblequipo.EqpID=tblpuesto.PtoEqp)
inner join tblplaza on (tblpuesto.PtoID=tblplaza.PzaPto)
inner join tblpersona on (tblplaza.PzaPrsn=tblpersona.PrsnID)
where tblequipo.EqpID=idEquipo and tblpuesto.PtoID=Puesto;
declare equipoSuperiorDeMiJefe cursor for select tblequipo.EqpEqpSup
from tblequipo
inner join tblpuesto on(tblequipo.EqpID=tblpuesto.PtoEqp)
where tblpuesto.PtoID=Puesto;
if tipo="jefe"
then
declare done int default 0;
declare continue handler for sqlstate '02000' set done=1;
open jefeONo;
begin
repeat
fetch jefeONo into varJefe;
until done end repeat;
end;
close jefeONo;
if varJefe=1
then
declare done int default 0;
declare continue handler for sqlstate '02000' set done=1;
open equipoSuperiorDeMiJefe;
begin
repeat
fetch equipoSuperiorDeMiJefe into eqpSupJefe;
until done end repeat;
end;
close equipoSuperiorDeMiJefe;
call getLider(eqpSupJefe);
else
if varJefe=0
then
call getLider(idEquipo);
end if;
end if;
end if;
end $$
delimiter ;
Problem is in the part as pointed below, where you are trying to declare a local variable inside IF .. ELSE block. You can set the variable inside if .. else block but you should declare them in the beginning
if varJefe=1
then
declare done int default 0; <-- Here
You should declare the variable at the beginning like
create procedure getListPrsn(IN idEquipo INT, IN tipo char, IN Puesto INT)
begin
declare varJefe int;
declare eqpSupJefe int;
declare done int default 0; <-- declare it here
A DECLARE has to be at the beginning of a BEGIN ... END block. You can either move them to the beginning of the procedure (the variable declaration has to be before the cursors, the handler declaration has to be after them):
delimiter $$
create procedure getListPrsn(IN idEquipo INT, IN tipo char, IN Puesto INT)
begin
declare varJefe int;
declare eqpSupJefe int;
declare done int default 0;
declare jefeONo cursor for select tblpuesto.PtoLiderEqp from tblequipo
inner join tblpuesto on (tblequipo.EqpID=tblpuesto.PtoEqp)
inner join tblplaza on (tblpuesto.PtoID=tblplaza.PzaPto)
inner join tblpersona on (tblplaza.PzaPrsn=tblpersona.PrsnID)
where tblequipo.EqpID=idEquipo and tblpuesto.PtoID=Puesto;
declare equipoSuperiorDeMiJefe cursor for select tblequipo.EqpEqpSup
from tblequipo
inner join tblpuesto on(tblequipo.EqpID=tblpuesto.PtoEqp)
where tblpuesto.PtoID=Puesto;
declare continue handler for sqlstate '02000' set done=1;
if tipo="jefe"
then
open jefeONo;
begin
repeat
fetch jefeONo into varJefe;
until done end repeat;
end;
close jefeONo;
if varJefe=1
then
open equipoSuperiorDeMiJefe;
begin
set done = 0;
repeat
fetch equipoSuperiorDeMiJefe into eqpSupJefe;
until done end repeat;
end;
close equipoSuperiorDeMiJefe;
call getLider(eqpSupJefe);
else
if varJefe=0
then
call getLider(idEquipo);
end if;
end if;
end if;
end $$
delimiter ;
or put them after the BEGIN statements before the REPEAT statements:
delimiter $$
create procedure getListPrsn(IN idEquipo INT, IN tipo char, IN Puesto INT)
begin
declare varJefe int;
declare eqpSupJefe int;
declare jefeONo cursor for select tblpuesto.PtoLiderEqp from tblequipo
inner join tblpuesto on (tblequipo.EqpID=tblpuesto.PtoEqp)
inner join tblplaza on (tblpuesto.PtoID=tblplaza.PzaPto)
inner join tblpersona on (tblplaza.PzaPrsn=tblpersona.PrsnID)
where tblequipo.EqpID=idEquipo and tblpuesto.PtoID=Puesto;
declare equipoSuperiorDeMiJefe cursor for select tblequipo.EqpEqpSup
from tblequipo
inner join tblpuesto on(tblequipo.EqpID=tblpuesto.PtoEqp)
where tblpuesto.PtoID=Puesto;
if tipo="jefe"
then
open jefeONo;
begin
declare done int default 0;
declare continue handler for sqlstate '02000' set done=1;
repeat
fetch jefeONo into varJefe;
until done end repeat;
end;
close jefeONo;
if varJefe=1
then
open equipoSuperiorDeMiJefe;
begin
declare done int default 0;
declare continue handler for sqlstate '02000' set done=1;
repeat
fetch equipoSuperiorDeMiJefe into eqpSupJefe;
until done end repeat;
end;
close equipoSuperiorDeMiJefe;
call getLider(eqpSupJefe);
else
if varJefe=0
then
call getLider(idEquipo);
end if;
end if;
end if;
end $$
delimiter ;
I fix my SP
We have to check the declaration order
1.- variables
2.- conditions
3.- cursors
4.- handlers
delimiter $$
create procedure getListPrsn(IN idEquipo INT, IN tipo CHAR, IN Puesto INT)
begin
declare varJefe int;
declare eqpSupJefe int;
declare done int default 0;
case tipo
when "jefe" then
begin
declare jefeONo cursor for select tblpuesto.PtoLiderEqp from tblequipo
inner join tblpuesto on (tblequipo.EqpID=tblpuesto.PtoEqp)
inner join tblplaza on (tblpuesto.PtoID=tblplaza.PzaPto)
inner join tblpersona on (tblplaza.PzaPrsn=tblpersona.PrsnID)
where tblequipo.EqpID=idEquipo and tblpuesto.PtoID=Puesto;
declare continue handler for sqlstate '02000' set done=1;
open jefeONo;
repeat
fetch jefeONo into varJefe;
until done end repeat;
close jefeONo;
set done=0;
if varJefe=1
then
begin
declare equipoSuperiorDeMiJefe cursor for select tblequipo.EqpEqpSup
from tblequipo
inner join tblpuesto on(tblequipo.EqpID=tblpuesto.PtoEqp)
where tblpuesto.PtoID=Puesto;
declare continue handler for sqlstate '02000' set done=1;
open equipoSuperiorDeMiJefe;
repeat
fetch equipoSuperiorDeMiJefe into eqpSupJefe;
until done end repeat;
close equipoSuperiorDeMiJefe;
call getLider(eqpSupJefe);
end;#begin del if varJefe=1
else
if varJefe=0
then
call getLider(idEquipo);
end if;
end if;
end;#begin del case JEFE
end case;
end $$
delimiter ;