我得给IT部门的所有员工加薪20%。在MySQL工作台中。
创建下表: Emp(E_ID,E_Name,E_Dept,E_Salary)
将适当的数据插入Emp表中。属性E_Dept包含的值如( I.T.,Accounts,Sales)..编写一个PL-SQL游标,用于递增I.T部门员工的工资。增加了20%。
我写了这个查询,
create procedure up()
begin
DECLARE v_employee_id INT;
enter code hereDECLARE v_salary NUMERIC(8,2);
DECLARE v_last_emp INT DEFAULT 0;
delcare emp_cur cursor for select empid,salary from employee where dept = IT for update;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET v_last_emp=1;
START TRANSACTION;
open emp_cur;
emp_loop: LOOP
fetch emp_cur into v_employee_id, v_salary;
if v_last_emp then
leave emp_loop;
end if
update employee set salary = salary + salary * 0.20 where current of emp_cur;
end loop emp_loop;
close emp_cur;
set v_last_emp =0;
end;
发布于 2021-08-13 18:48:29
不要使用游标,这是基本的SQL。只需执行以下操作,假设这是一个文本字段,我为dept
过滤器添加了单引号,但如果不是,请相应地进行调整:
UPDATE employee SET salary = 1.2 * salary WHERE dept = 'IT';
发布于 2021-08-13 18:51:48
不需要将其作为过程或循环运行
但是您的代码中存在一些问题
create procedure up()
begin
DECLARE v_employee_id INT;
#enter code here
DECLARE v_salary NUMERIC(8,2);
DECLARE v_last_emp INT DEFAULT 0;
declare emp_cur cursor for select empid,salary from employee where dept = 'IT' for update;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET v_last_emp=1;
START TRANSACTION;
open emp_cur;
emp_loop: LOOP
fetch emp_cur into v_employee_id, v_salary;
if v_last_emp then
leave emp_loop;
end if;
UPDATE employee
SET
salary = salary + (salary * 0.20)
WHERE
empid = v_employee_id;
end loop emp_loop;
close emp_cur;
set v_last_emp =0;
end;
https://stackoverflow.com/questions/68777065
复制相似问题