Michael L
Michael L

Reputation: 37

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 always 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?

Upvotes: 3

Views: 1279

Answers (1)

user330315
user330315

Reputation:

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.

Upvotes: 6

Related Questions