【问题标题】:store procedure not giving correct values存储过程没有给出正确的值
【发布时间】:2020-07-11 20:34:54
【问题描述】:

我正在尝试使用存储过程查找具有特定工资范围的员工数量,我得到了结果,但它们不等于简单查询的结果(下面的第二个查询),这意味着存储过程不是工作正常吗?

我希望有人能帮我解决这个问题。

-- ANSWER : NULL /没有低于 38000 的工资金额,但是使用存储过程我得到 967330

其次,有没有其他方法可以缩短存储过程中的 case when 语句?在当前版本中,我每次都必须添加子查询。

数据库名称:员工 栏目名称:工资 employee_id = emp_no

DELIMITER $$
CREATE PROCEDURE SALARY_RANGE
(INOUT no_employees INT, IN salary INT)
BEGIN
CASE
WHEN (salary <= 38000) THEN (SELECT COUNT(emp_no) INTO no_employees 
                        FROM salaries 
                            WHERE salary <= 38000);
                            
WHEN (salary<= 68000) THEN (SELECT COUNT(emp_no) INTO no_employees 
                        FROM salaries
                            WHERE  salary <= 68000);
WHEN (salary <= 88000) THEN (SELECT COUNT(emp_no) INTO no_employees 
                        FROM salaries
                            WHERE  salary <= 88000);
WHEN (salary <= 100000) THEN (SELECT COUNT(emp_no) INTO no_employees 
                        FROM salaries
                            WHERE  salary <=  100000);  
ELSE (SELECT COUNT(emp_no) INTO no_employees 
FROM salaries WHERE salary > 100000);
END CASE;
END$$

CALL SALARY_RANGE(@finding_salary_range_count,'38000')$$
select @finding_salary_range_count;


-- to check the if the answer matched the above store procedure
select count(case when salary <= 88000 then emp_no else null end) as count_sal
from salaries;

【问题讨论】:

    标签: mysql sql stored-procedures


    【解决方案1】:

    我希望这样的代码:

    CREATE PROCEDURE SALARY_RANGE (
        OUT out_num_employees INT,
        IN in_salary INT
    )
    BEGIN
        SELECT COUNT(emp_no) INTO out_num_employees 
        FROM salaries 
        WHERE salary <= in_salary or in_salary IS NULL
    END;
    

    我认为不需要多个 SELECT 语句。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多