在另一个函数中添加来自 MySQL 函数的两个输出的问题
Posted
技术标签:
【中文标题】在另一个函数中添加来自 MySQL 函数的两个输出的问题【英文标题】:Issue in adding two outputs from MySQL function inside another function 【发布时间】:2011-01-13 17:30:51 【问题描述】: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$$
问题是它在行给出错误:
SET TotalCostOfEmployees = TotalCostOfEmployees + ( F_TotalManDaysPerEmployee(e_id,id)*(F_GetEmployeeGradeSal(e_id));
这是错误:
错误代码:1064 你有一个错误 你的 SQL 语法;检查手册 对应你的 mysql 服务器 使用正确语法的版本 靠近 ';离开员工;如果结束;
set TotalCostOfEmployees = TotalCostOfEmploy' at line 12
【问题讨论】:
【参考方案1】:使用SELECT ... INTO
子句
SELECT TotalCostOfEmployees + ( F_TotalManDaysPerEmployee(e_id,id)*(F_GetEmployeeGradeSal(e_id))
INTO TotalCostOfEmployees;
【讨论】:
嘿,谢谢..这帮助了我....我是 mysql 的新手....所以犯了一个错误:) @Joy np。请记住接受您问题的答案:-) 对不起,我想我在这里发布了我的另一个问题,我在错误的部分中遇到了问题,你能照顾一下吗?【参考方案2】:你为什么要为此使用光标?
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
【讨论】:
【参考方案3】:我通过这种方式解决了它:
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$$
【讨论】:
以上是关于在另一个函数中添加来自 MySQL 函数的两个输出的问题的主要内容,如果未能解决你的问题,请参考以下文章
Visual Basic:如何将一个函数的输出包含在另一个函数中
我如何在另一个函数中使用retailId(来自flatMapLatest)
Swift - 在另一个函数中使用来自 UISlider IBAction 的值