procedure返回null值

时间:2017-08-29 10:39:15

标签: mysql procedure

我在下面写了一个简单的演示程序是代码

delimiter //
create procedure test2(IN boss int, out name varchar(50))
begin
select Name into name  from employee where Reports_to=boss;
end //

当我将此过程称为CALL test2(2,@name)时,它返回null

虽然表中包含所有值 -

mysql> select * from employee;
+--------+--------+--------+-------------+------------+
| Emp_Id | Name   | Salary | Department  | Reports_to |
+--------+--------+--------+-------------+------------+
|      1 | Alok   |  50000 | RnD         |       NULL |
|      2 | Mahesh |  40000 | Development |          1 |
|      3 | Ramesh |  40000 | Development |          1 |
|      4 | Bimal  |  30000 | Sales       |          2 |
|      5 | Javed  |  20000 | Marketing   |          4 |
|      6 | Mukesh |  35000 | Accounts    |          3 |
+--------+--------+--------+-------------+------------+

以及如果我写CALL test2(1,@name)它会显示错误

  

结果包含多行

如何解决这两个错误......?

1 个答案:

答案 0 :(得分:2)

以下查询失败,因为它无法为变量分配多个值:

select Name into name  from employee where Reports_to=boss;

你想要的是像GROUP_CONCAT这样会返回逗号分隔值的东西。请尝试以下方法:

SELECT GROUP_CONCAT(name) INTO names
FROM employee
WHERE reports_to = boss;