Search code examples
mysqlselectreturnrowprocedure

How to get a single row in Mysql stored procedure with select


I have been sreaching around and I could not find an answer to this question. It may be very simple or it could a bug, so any help to point out what I am doing wrong, it's appreciated in advance.

I have a table where id is unique:

Table school
---------------------------
id  name     class   fitness
---------------------------
1    Joe      4      healthy
2    Alice    7       good
3    Bob      10     excellent

and the stored procedure is:

CREATE PROCEDURE checkid(IN ID INT)
BEGIN
    SELECT * FROM school WHERE id = ID;
END

executing the above procedure with following command:

CALL checkid(2)

returns the entire table. if I changed the SELECT statement to:

CREATE PROCEDURE checkid(IN ID INT)
BEGIN
     SELECT * FROM school WHERE id = ID LIMIT 1;
END

I get the first row of table as the return value like this which is wrong.


id  name     class   fitness
---------------------------
1    Joe      4      healthy

how can I fix this?


Solution

  • Thanks everyone for your help. after trying many different things and further search through the net. I found this link that help to resolve the issue.

    How to get a row by ID in a MySQL Stored Procedure?

    when I changed the input parameter name to something else, it worked.