开发者

Issue in adding two outputs from MySQL function inside another function

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; 开发者_如何学Python 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$$
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜