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

标签 mysql database stored-procedures cursor

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

最佳答案

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

关于MySql 程序用于更新整个表的奖金计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53691857/

相关文章:

php - 使用 join codeigniter 显示不在其他表中的数据

mysql - 将查询从 mysql 转换为 php laravel?

MySql查询: Show Columns as Rows from 1 table

c# - SQLite无法连接到另一台PC上的数据库

sql-server - 使用存储过程将一些数据插入表中

mysql - 拆分字符串并循环遍历 MySQL 存储过程中的值

MySQL:添加到计数大小写...然后

java - 如果数据库调用来自多台机器,Spring 事务能否提供必要的互斥

mysql - 如果主从具有不同的数据库以防Mysql复制,如何重新同步Mysql DB?

ms-access - Access 2007 触发器和程序等效项?