[Err] 1242 - Subquery returns more than 1 row

1、错误描述

[SQL]SELECT queryStuName(2016010101);
[Err] 1242 - Subquery returns more than 1 row

2、错误原因

BEGIN
  DECLARE stuName VARCHAR(100);
	SET stuName = (SELECT stu_name FROM t_student_info WHERE stu_no = stu_no);
	RETURN stuName;
END

SELECT queryStuName(2016010101);

       由于字段名和参数名一样,这样就会导致查询语句默认为字段名=字段名,查询出来的结果为多条


3、解决办法

DROP FUNCTION IF EXISTS `queryStuName`;

CREATE DEFINER = `root`@`localhost` FUNCTION `queryStuName`(`stuNo` int)
 RETURNS varchar(100)
BEGIN
  DECLARE stuName VARCHAR(100);
	SELECT stu_name FROM t_student_info WHERE stu_no = stuNo INTO stuName;
	RETURN stuName;
END;


你可能感兴趣的:(MySQL,mysql)