【问题标题】:MySql Procedure For Bonus Calculation Updating the Entire Table更新整个表的奖金计算的 MySql 程序
【发布时间】:2018-12-09 11:29:49
【问题描述】:

尝试在mysql中编写一个简单的程序来根据部门ID计算奖金。以下是我正在做的事情。问题是当我给我的程序一个特定的部门 id 时,它会用相同的薪水值更新整个表的薪水,而不是将自己限制为提供的部门 id。花了很多时间,但无法弄清楚问题所在。

create table employees(emp_id integer,dept_id int(4),emp_name varchar(10), salary float(11));
alter table employees add primary key (emp_id);
insert into employees values(1, 1,'A1',30);
insert into employees values(2, 2,'R1', 40);
insert into employees values(3, 3,'A2', 50);
insert into employees values(4, 4,'S1', 60);
insert into employees values(5, 1,'A3', 700);


delimiter $$
create procedure calculate_bonus(in in_dept_id int)
begin
declare done int default false;
declare emp_id integer;
declare dept_id int(4);
declare emp_name varchar(10); 
declare new_salary float(11);
declare hike float(11);
declare c1 cursor for
select * from employees;
Declare continue handler for not found set done = TRUE;

open c1;
read_cursor: LOOP
fetch c1 into emp_id, dept_id, emp_name, new_salary;
if done then
leave read_cursor;
end if;


if(dept_id = in_dept_id) then
select case dept_id
when 1 then 10
when 2 then 20
when 3 then 30
else 40
end
into hike;

set new_salary = new_salary + (new_salary*hike/100);
select concat("salary",new_salary);

update employees
set salary = new_salary where dept_id = in_dept_id;
select concat("dept_id",dept_id, in_dept_id);
end if;
end LOOP read_cursor;
close c1;
end
$$

call calculate_bonus(3);
select * from employees;

我得到的输出是:

salary65
dept_id33
1   1   A1  65
2   2   R1  65
3   3   A2  65
4   4   S1  65
5   1   A2  65

【问题讨论】:

  • 如果您只对 dept_id = 3 感兴趣,您认为选择所有员工到您的游标中是否有意义,并且通过游标循环更新所有员工的 dept_id evrey 时间是否有意义?还有不要为参数和声明的变量赋予与列名相同的名称。
  • @P.Salmon:感谢您的意见。根据您的第一个建议,我已将我的 select 语句更改为:为 select * from employees where dept_id=in_dept_id 声明 c1 游标;现在它没有更新任何记录。根据我的理解,我正在编写游标以获取具有匹配部门 id 的所有记录并更新每个记录的薪水。因此使用游标。我的想法是在光标中保存所有匹配的部门 ID。我更想知道当前逻辑出了什么问题。
  • @P.Salmon 您将变量名与列名分开的想法似乎是主要罪魁祸首之一,但现在,具有相同部门 id 的记录会随着最后一条记录的薪水增加而更新对于那个部门ID。例如1 1 A1 770 , 2 2 R1 40, 3 3 A2 50, 4 4 S1 60, 5 1 A3 770, A1的初始工资是30,但现在它随着A3的增加工资而更新

标签: mysql database stored-procedures cursor


【解决方案1】:
DROP TABLE IF EXISTS T;
create table t(emp_id integer,dept_id int(4),emp_name varchar(10), salary float(11));
alter table t add primary key (emp_id);
insert into t values(1, 1,'A1',30);
insert into t values(2, 2,'R1', 40);
insert into t values(3, 3,'A2', 50);
insert into t values(4, 4,'S1', 60);
insert into t values(5, 1,'A3', 700);

drop procedure if exists p;
delimiter $$

create procedure p(in in_dept_id int)
begin
declare done int default false;
declare vemp_id integer;
declare vdept_id int(4);
declare vemp_name varchar(10); 
declare vnew_salary float(11);
declare vhike float(11);
declare c1 cursor for select * from t where dept_id = in_dept_id;
Declare continue handler for not found set done = TRUE;

open c1;
read_cursor: LOOP
fetch c1 into vemp_id, vdept_id, vemp_name, vnew_salary;
if done then
    leave read_cursor;
end if;


select case vdept_id
    when 1 then 10
    when 2 then 20
    when 3 then 30
    else 40
    end
    into vhike;

    set vnew_salary = vnew_salary + (vnew_salary*vhike/100);
    select concat("salary",vnew_salary);

    update t
        set salary = vnew_salary where dept_id = in_dept_id;
    select concat("dept_id",vdept_id, in_dept_id);


end LOOP read_cursor;
close c1;
end $$

call p(3);
call p(1);
select * from t;

+--------+---------+----------+--------+
| emp_id | dept_id | emp_name | salary |
+--------+---------+----------+--------+
|      1 |       1 | A1       |     33 |
|      2 |       2 | R1       |     40 |
|      3 |       3 | A2       |     65 |
|      4 |       4 | S1       |     60 |
|      5 |       1 | A3       |    770 |
+--------+---------+----------+--------+
5 rows in set (0.00 sec)

注意我已经对声明的变量进行了唯一命名,修改了游标选择以仅选择我感兴趣的 dept_id,删除了现在多余的 if 语句并进一步限定了员工 ID 上的更新语句。 该过程(可能)和游标(绝对)是不必要的(除非您被明确告知这样做),并且可以通过单个更新语句来实现相同的结果,例如

update t
    set salary = salary + (salary * case dept_id
            when 1 then 10
            when 2 then 20
            when 3 then 30
            else 40
            end / 100)
        where dept_id = 3;

通过简单的更改来接受参数值而不是硬编码的 3,这将是您在过程中需要的所有代码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-30
    • 1970-01-01
    • 1970-01-01
    • 2021-02-25
    • 2016-12-22
    • 1970-01-01
    • 2015-07-12
    相关资源
    最近更新 更多