简体   繁体   English

MySQL使用游标触发并循环更新错误值

[英]MySQL trigger with cursor and loop updating the wrong values

Hey im having problems setting the correct value in an update statement inside a loop triggered by an update on another table. 嘿,在另一个表的更新触发的循环内,在更新语句中设置正确的值时遇到问题。

I have 2 tables, employees and base_wage. 我有2个表,employees和base_wage。 The base wage is what new employees will receive as standard for their job position. 基本工资是新员工将获得的标准职位。 In the employees table there is a salary column which typically is the base wage, but can sometimes be higher than the base wage. 在雇员表中,有一个工资列,通常是基本工资,但有时可能会高于基本工资。 For instance if a guy has been in a position for many years he might get a higher salary compared to a guy that just got the job. 例如,如果一个人已经担任了多年,那么他可能会比刚得到这份工作的人获得更高的薪水。

I have made a trigger AFTER UPDATE ON the base_wage table which sets the new base wage for every employee with that position. 我在base_wage表上进行了更新后的触发器,该触发器为具有该职位的每个员工设置了新的基本工资。 The trigger should also take into account the extra salary that some employees might have and add that to their salary ontop of the new base wage. 触发因素还应考虑某些员工可能拥有的额外工资,并将其添加到新基本工资之上。

The trigger works, but it doesn't add the extra salary to those employees that gets more than the base wage. 触发器可以工作,但不会为获得高于基本工资的员工增加额外的工资。

I have created a Fiddle here: http://sqlfiddle.com/#!2/cfe116/1 我在这里创建了一个小提琴: http ://sqlfiddle.com/#!2/cfe116/1

In the fiddle the base wage for a manager is 150, and i have added two managers to the employees table, one with a salary of 150 (the base) and one that gets 170 (20 more than base). 在小提琴中,经理的基本工资是150,我在雇员表中增加了两个经理,一个经理的工资为150(基本工资),另一个为170(基本工资高20)。 If i now want to change the base wage for managers to 160, the first manager should get 160 as salary and the second one should get his salary (170) minus the old base wage (150) plus the new base wage = 180. Right now both managers salary changes to 160 for some odd reason. 如果我现在要将经理的基本工资更改为160,则第一位经理应获得160作为工资,第二位经理应获得其工资(170)减去旧的基本工资(150)加上新的基本工资= 180。现在,由于某种奇怪的原因,两位经理的薪水都改为160。

So any idea why this doesn't work? 那么,为什么不起作用呢? Am i missing something here? 我在这里想念什么吗?

Thanks in advance :) 提前致谢 :)

Problem is here UPDATE employees SET salary = mTotalSalary WHERE jobTitel = OLD.position; 问题是这里UPDATE employees SET salary = mTotalSalary WHERE jobTitel = OLD.position;

You are updating the employee table using the position and it will update each employee having the same position with the same value you will need to use the employeeid into consideration. 您正在使用该职位更新员工表,它将使用具有考虑使用employeeid所需的相同值的相同职位更新每个员工。

First declare 首先声明

declare empId INT;

Then fetch the employee id into the cursor 然后将员工ID提取到游标中

SELECT salary,employeeId FROM employees WHERE jobTitel = OLD.position;

And use it in the declared variable 并在声明的变量中使用它

FETCH employeeCursor INTO mSalary,empId;

Finally change the update command as 最后将update命令更改为

UPDATE employees SET salary = mTotalSalary WHERE employeeId = empId;

Here is the fiddle http://sqlfiddle.com/#!2/b4840/1 这是小提琴http://sqlfiddle.com/#!2/b4840/1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM