Issue in adding two outputs from MySQL function inside another function - mysql

CREATE DEFINER=`root`#`localhost` FUNCTION `F_GetProjectCostPerEmployeeInProject`(id VARCHAR(20)) RETURNS DECIMAL(30,2)
BEGIN
DECLARE e_id VARCHAR(20);
DECLARE finished INT ;
DECLARE temp DECIMAL(30,2);
DECLARE temp2 DECIMAL(30,2);
DECLARE TotalCostOfEmployees DECIMAL(30,2);
DECLARE cur CURSOR FOR SELECT DISTINCT e_id FROM project_employee WHERE project_id=id;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET finished = 1;
emploop : LOOP
FETCH cur INTO e_id;
IF finished =1 ;
LEAVE emploop;
END IF ;
SET TotalCostOfEmployees = TotalCostOfEmployees + ( F_TotalManDaysPerEmployee(e_id,id)*(F_GetEmployeeGradeSal(e_id));
END LOOP emploop;
RETURN TotalCostOfEmployees;
END$$
The problem is its giving error at line :
SET TotalCostOfEmployees = TotalCostOfEmployees + ( F_TotalManDaysPerEmployee(e_id,id)*(F_GetEmployeeGradeSal(e_id));
This is the error :
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 '; leave emploop; end if ;
set TotalCostOfEmployees = TotalCostOfEmploy' at line 12

Use the SELECT ... INTO clause
SELECT TotalCostOfEmployees + ( F_TotalManDaysPerEmployee(e_id,id)*(F_GetEmployeeGradeSal(e_id))
INTO TotalCostOfEmployees;

Why are you using a cursor for this at all?
SELECT SUM(F_TotalManDaysPerEmployee(e_id, id) * F_GetEmployeeGradeSal(e_id)) AS TotalCostOfEmployees
FROM (
SELECT DISTINCT e_id
FROM project_employee
WHERE project_id = id
) q

I solved it via this way:
enter code BEGIN DECLARE e_id VARCHAR(20);
DECLARE finished INT ;
DECLARE salary DECIMAL(30,2);
DECLARE TotalCostOfEmployees DECIMAL(30,2) ;
DECLARE cur CURSOR FOR SELECT DISTINCT Emp_code FROM project_employee WHERE project_id=p_id;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET finished = 1;
SET finished = 0;
OPEN cur;
SET TotalCostOfEmployees =0.0;
emploop : LOOP
FETCH cur INTO e_id;
IF finished = 1 THEN
LEAVE emploop;
END IF ;
SELECT COALESCE( (F_TotalManDaysPerEmployee(e_id,p_id)* F_GetEmployeeGradeSal(e_id))/22,0.0)
INTO salary;
SET TotalCostOfEmployees = TotalCostOfEmployees + salary;
/*SELECT (TotalCostOfEmployees + ifnull(salary,0.0)) into TotalCostOfEmployees; */
END LOOP emploop;
CLOSE cur;
RETURN TotalCostOfEmployees;
END$$

Related

MySQL stored procedure syntax error for cursor select

DROP PROCEDURE CREATE_DUPLICATE_PRODUCT;
DELIMITER $$
CREATE PROCEDURE `CREATE_DUPLICATE_PRODUCT`(IN `old_course_id` INT(25), IN `new_course_id`
INT(25))
BEGIN
DECLARE db_cursor CURSOR FOR SELECT lic.org_id, lic.course_id, lic.license_duration_typeid, lic.insert_date, lic.insert_by, lic.quantity FROM cdp_organization_licenses as lic JOIN cdp_organization_license_settings as sett ON sett.org_license_id = lic.id JOIN cdp_organization_purchases as pur ON pur.org_id = lic.org_id AND pur.course_id = lic.course_id JOIN cdp_organizations as org ON org.org_id = lic.org_id WHERE lic.status_id = 1 AND org.status_id = 1;
DECLARE #org_id INT;
DECLARE #course_id INT;
DECLARE #license_typeid INT;
DECLARE #insert_date INT;
DECLARE #insert_by INT;
DECLARE #quantity INT;
OPEN db_cursor;
FETCH NEXT FROM db_cursor INTO #org_id, #course_id,
#license_duration_typeid, #insert_date, #insert_by, #quantity;
WHILE ##FETCH_STATUS = 0
BEGIN
--Do stuff with scalar values
FETCH NEXT FROM db_cursor INTO #org_id, #course_id,
#license_duration_typeid, #insert_date, #insert_by, #quantity;
INSERT INTO cdp_organization_licenses_test
SET
org_id = #org_id,
course_id = #course_id,
license_duration_typeid = #license_duration_typeid,
insert_date = #insert_date,
insert_by = #insert_by,
quantity = #quantity;
END;
CLOSE db_cursor;
DEALLOCATE db_cursor;
END$$
DELIMITER ;
I am getting this error:
#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 '#org_id INT;
DECLARE #course_id INT;
DECLARE #license_typeid INT; ' at line 5
What can I do to resolve this?
You are mixing sql serv and mysql , that can not work
DROP PROCEDURE CREATE_DUPLICATE_PRODUCT;
DELIMITER $$
CREATE PROCEDURE `CREATE_DUPLICATE_PRODUCT`(IN `old_course_id` INT(25), IN `new_course_id`
INT(25))
BEGIN
DECLARE _org_id INT;
DECLARE _course_id INT;
DECLARE _license_typeid INT;
DECLARE _insert_date INT;
DECLARE _insert_by INT;
DECLARE _quantity INT;
DECLARE finished INTEGER DEFAULT 0;
DECLARE db_cursor CURSOR FOR
SELECT lic.org_id, lic.course_id, lic.license_duration_typeid, lic.insert_date, lic.insert_by, lic.quantity
FROM cdp_organization_licenses as lic
JOIN cdp_organization_license_settings as sett ON sett.org_license_id = lic.id
JOIN cdp_organization_purchases as pur ON pur.org_id = lic.org_id AND pur.course_id = lic.course_id
JOIN cdp_organizations as org ON org.org_id = lic.org_id
WHERE lic.status_id = 1 AND org.status_id = 1;
DECLARE CONTINUE HANDLER
FOR NOT FOUND SET finished = 1;
OPEN db_cursor;
getinfo: LOOP
BEGIN
FETCH NEXT FROM db_cursor INTO _org_id, _course_id,
_license_duration_typeid, _insert_date, _insert_by, _quantity;
IF finished = 1 THEN
LEAVE getEmail;
END IF;
INSERT INTO cdp_organization_licenses_test
SET
org_id = _org_id,
course_id = _course_id,
license_duration_typeid = _license_duration_typeid,
insert_date = _insert_date,
insert_by = _insert_by,
quantity = _quantity;
END;
END LOOP getinfo;
CLOSE db_cursor;
END$$
DELIMITER ;
But t´you can simply do a
INSERT INTO cdp_organization_licenses_test
SELECT lic.org_id, lic.course_id, lic.license_duration_typeid, lic.insert_date, lic.insert_by, lic.quantity
FROM cdp_organization_licenses as lic
JOIN cdp_organization_license_settings as sett ON sett.org_license_id = lic.id
JOIN cdp_organization_purchases as pur ON pur.org_id = lic.org_id AND pur.course_id = lic.course_id
JOIN cdp_organizations as org ON org.org_id = lic.org_id
WHERE lic.status_id = 1 AND org.status_id = 1;
That makes all with out the loop
Don't DECLARE variables with # at the beginning of their names. Those are session variables, don't need to be declared, and have session-wide scope,

Mysql Store procedure stops iteration after some rows

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

Mysql stored procedure not returning any values

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

Cursor to copy distinct record from one table to another

I want to copy all recoreds from temp1 table to anoter two tables I am using cursor for this .
DELIMITER //
CREATE PROCEDURE cpyQ()
BEGIN
DECLARE g_id INT DEFAULT 0;
DECLARE v_fn varchar(100);
DECLARE v_ln varchar(100);
DECLARE v_email varchar(100);
declare tcursor for select distinct mailid,fname,lname from temp1;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET flag = 1;
OPEN tcursor;
REPEAT
FETCH cursor into v_fn,v_ln, v_email;
insert into atom(type) values('Person');
SET g_id = LAST_INSERT_ID();
insert into user(id,fname,lname,mailid) values(g_id,v_fname,v_lname,v_email);
END REPEAT;
CLOSE tcursor;
END//
DELIMITER
this code is showing error
MySQL said: Documentation
#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 'for select distinct mailid,fname,lname from temp1;
DECLARE CONTINUE HANDLE' at line 8
How to resolve this
You have multiple errors in your syntax and don't exit the loop. Try this?
CREATE PROCEDURE cpyQ()
BEGIN
DECLARE g_id INT DEFAULT 0;
DECLARE v_fn varchar(100);
DECLARE v_ln varchar(100);
DECLARE v_email varchar(100);
DECLARE done INT DEFAULT FALSE;
declare tcursor cursor for select distinct mailid,fname,lname from temp1;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN tcursor;
read_loop: LOOP
FETCH tcursor into v_fn,v_ln, v_email;
if done then
LEAVE read_loop;
END IF;
insert into atom(type) values('Person');
SET g_id = LAST_INSERT_ID();
insert into user(id,fname,lname,mailid) values(g_id,v_fn,v_ln,v_email);
END LOOP;
CLOSE tcursor;
END
I tried this query and find this is working
insert into atom(id,type) select id,'Person' from user1;
INSERT INTO user( id, fname, lname, mailid ) SELECT id, fname, lname, mailid FROM user1;

Sytax error in mysql stored procedure

What is wrong in the syntax of the below stored procedure in MySQL ?
CREATE PROCEDURE curdemo()
BEGIN
DECLARE #isbn varchar(17)
DECLARE #count int
DECLARE #price float(6,2)
DECLARE #totalbookpriceValue float(6,2)
DECLARE totalbookprice CURSOR
STATIC FOR
(SELECT ISBN,COUNT(ISBN) FROM applieddb.cart c GROUP BY ISBN)
OPEN totalbookprice
IF ##CURSOR_ROWS > 0 BEGIN
FETCH NEXT FROM cur_emp INTO #isbn,#count
WHILE ##Fetch_status = 0
SELECT ISBN,PRICE FROM book INTO #price WHERE ISBN = #isbn
PRINT 'Total Book Price is' + #price * #count
FETCH NEXT FROM cur_emp INTO #isbn,#count
END
END
CLOSE totalbookprice
DEALLOCATE totalbookprice
SET NOCOUNT OFF
END
ERROR Message:
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 '#isbn varchar(17) DECLARE #count int DECLARE #price float(6,2) DECLARE #totalboo' at line 3
Changing the mysql query to the below also did not help and gave the following error:
DECLARE for local (without #) or SET for session (with #)
***The solution to the above problem is as below:***
CREATE DEFINER=`root`#`localhost` PROCEDURE `calculateTotalPrice`()
BEGIN
DECLARE done INT default 0;
DECLARE _isbn varchar(17);
DECLARE _count int;
DECLARE _price float(6,2);
DECLARE loop_cntr INT DEFAULT 0;
DECLARE _totalbookpriceValue float(6,2);
DECLARE totalbookprice CURSOR FOR SELECT ISBN,COUNT(ISBN) FROM applieddb.cart c GROUP BY ISBN;
DECLARE CONTINUE HANDLER FOR NOT FOUND set done= TRUE;
OPEN totalbookprice;
the_loop: LOOP
FETCH totalbookprice into _isbn,_count;
SET _price= (SELECT b.price from book b where b.ISBN = _isbn);
SET _totalbookpriceValue= _price*_count;
SELECT _totalbookpriceValue;
IF done THEN
CLOSE totalbookprice;
LEAVE the_loop;
END IF;
-- count the number of times looped
SET loop_cntr = loop_cntr + 1;
END LOOP the_loop;
END