错误代码:1172。结果包含多行错误

Error Code: 1172. Results consisted in more than one row error

所以我有 2 个 table 名为 Employee(employeeID, salary) 和 Tax(employeeID, tax, revisedSalary),我的目标是通过提取Employee Table 中 Salary 列的值,执行计算并将结果存储在 Tax table 中,所有这些都使用存储过程

这是我尝试完成的,但到目前为止没有成功,我收到 'Results consisted in more than one row' 错误:

delimiter //
create procedure calcTax()
begin
  declare tax, salary, revisedSalary double default 0.0;
  declare i int;
  select Salary from Employee into salary;
  while i <= salary do
  if salary >= 0.0 and salary<= 10000.0
  then
    set tax = salary * 10/100;
     set revisedSalary = salary - tax;
  end if;
    insert into Tax (tax, revisedSalary) values(tax, revisedSalary);
    set i = i + 1;
end while;
end //
delimiter ;

除了您不能将结果集存储在变量中这一事实之外,您的问题没有多大意义,因为您正在尝试计算每位员工的税金和薪水,并且您插入的行没有参考员工。也不需要 while 循环,同样可以通过简单的插入 select

来实现
DROP TABLE IF EXISTS T,tax;
CREATE TABLE T
(employeeid int, salary int);
create table tax
(employeeid int, tax int , revisedsalary int);

insert into t values
(1,0),(2,5000),(3,7000),(4,15000);

insert into tax 
select employeeid,
         salary * .10,
         salary - (salary * .10)
from t
where salary > 0 and salary <= 10000;

select * from tax;

+------------+------+---------------+
| employeeid | tax  | revisedsalary |
+------------+------+---------------+
|          2 |  500 |          5000 |
|          3 |  700 |          7000 |
+------------+------+---------------+
2 rows in set (0.001 sec)

如果你必须我们一个sp

delimiter $$
create procedure p()
begin
 insert into tax 
    select employeeid,
             salary * .10,
             salary - (salary * .10)
    from t
    where salary > 0 and salary <= 10000;
    
    select * from tax;
end $$
delimiter ;

call p();