Result consisted of more than one row 错误的解决

时间:2022-06-06 10:10:04

创建MySql的存储过程时,发生“Result consisted of more than one row”的错误。

存储过程的代码如下:

1 CREATE PROCEDURE GetPetName(IN carID int, OUT petName char(10))
2 BEGIN
3 SELECT PetName into petName FROM `inventory` WHERE CarID = carID;
4 END

发生原因:SQL变量名不能和列名一样(SQL列名不区分大小写)。在上面代码中,它会认为CarID和carID是相同的。
因此,修改上述代码如下:

1 CREATE PROCEDURE GetPetName(IN id int, OUT pet char(10))
2 BEGIN
3 SELECT PetName into pet FROM `inventory` WHERE CarID = id;
4 END

当然,如果还有“Result consisted of more than one row”的问题,可以考虑修改代码如下:

1 CREATE PROCEDURE GetPetName(IN id int, OUT pet char(10))
2 BEGIN
3 SELECT PetName into pet FROM `inventory` WHERE CarID = id limit 1;
4 END