mysql程序,用于更新整个表的奖金计算

nszi6y05  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(295)

尝试在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
9wbgstp7

9wbgstp71#

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)

注意,我唯一地命名了声明的变量,修改了光标select,只选择我感兴趣的dept\u id,删除了现在多余的if语句,并进一步限定了employee id上的update语句。过程(可能)和游标(肯定)是不必要的(除非您被明确告知这样做),例如,可以通过单个update语句来实现相同的结果

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,这将是您在过程中需要的所有代码。

相关问题