MySQL - 函数保持返回null

时间:2016-04-16 22:34:34

标签: mysql

我正在尝试创建一个MySQL函数,将员工的时间存储在项目中并将其存储到v_emptime中,然后使用该变量计算总费用,如下所示:

delimiter //

create function F_COUNT_EDUCATION (p_empno char(6)) returns decimal(10, 2)
begin

    declare v_emptime decimal(5, 2);
    declare output decimal(10, 2);

    select sum(emptime) # There are multiple records for some, using sum() for total time
        into @v_emptime
        from empprojact
        where empno = p_empno;

    # if I return v_emptime here it will be null

    select (@v_emptime * comm + salary + bonus) expense 
        into @output
        from employee
        where empno = p_empno;

    return @output;

end //

delimiter ;

然而,问题是output将始终返回null。我已经无数次检查了数据,数据都有值。这意味着我可能会忽略一些非常简单的事情。任何帮助或见解将不胜感激,谢谢!

1 个答案:

答案 0 :(得分:0)

试试这个: 更改“@output”变量的名称。并更改查询:

select  @v_emptime = sum(emptime) 
from empprojact
where empno = p_empno;

并且:

select @newVariableName = (@v_emptime * comm + salary + bonus) expense
from employee where empno = p_empno;