MySQL stored procedure select returning incorrect values
I noticed that a select statement inside my stored procedure is always returning the same value, no matter what parameter I'm calling my stored procedure with. Here's the code:
DELIMITER $$
CREATE PROCEDURE TEST(IN id INT)
BEGIN
DECLARE x INT DEFAULT 0;
select id;
SELECT paper_id
INTO x
FROM Paper
WHERE ID = id
limit 1;
select x;
END$$
x alway开发者_开发问答s returns the same value no matter what id I call test with. I noticed the value of x is always the value of paper_id in the first row of my Paper table.
However, if I run the same query outside of the stored procedure, I get the expected value.
What is going wrong inside the stored procedure which skews that value?
I'm not that familiar with MySQL's stored procedures, but could it be that the expression WHERE ID = id
is evaluated as "all rows from Paper where the value in the column ID equals the value in the column ID" and simply ignores your parameter?
Try to rename your parameter to something that is different from the column name in your query.
精彩评论