Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

MySQL returning two different results from query vs stored procedure

I’m baffled as to why MySQL Workbench returns 7 (the correct answer) for this query

SELECT ScheduleLocationEventColorID 
FROM schedulelocationeventcolor 
WHERE ScheduleID = 1 AND LocationID = 1;

but returns 1 (incorrect) for the equivalent in a stored procedure.

CREATE PROCEDURE `test`(
    IN locationID INT,
    IN scheduleID INT
)
BEGIN
    DECLARE slecID INT;

    SELECT ScheduleLocationEventColorID 
    INTO slecID 
    FROM schedulelocationeventcolor 
    WHERE ScheduleID = scheduleID AND LocationID = locationID
    LIMIT 1;
    
    SELECT slecID;
END

-- calling the stored procedure here: 
CALL `test`(1, 1);

I tried changing the inputs in the stored procedure call, but I cannot figure out for the life of me why I keep getting a 1 when the result should be a 7.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You should name your procedure parameters something different from your column names. Column names are case-insensitive.

Right now your procedure has a query with this condition:

WHERE ScheduleID = ScheduleID AND LocationID = LocationID

Which evaluates as true AND true because comparing a column to itself is bound to be true (unless the column is NULL).

So with the LIMIT 1, the query returns the first row where both columns are not NULL.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading