Search code examples
mysqlstored-proceduresprocedure

MySQL OUT parameter returning NULL


I have a procedure in MySQL and I want the string Question_Description to be passed as an OUT parameter. However, every time I execute the procedure, it always returns NULL. How do I fix this? I have tried many different methods and none of them have worked.

DELIMITER $$
CREATE PROCEDURE `QuestionTitle`(IN `QID` INT(11), OUT `Description` VARCHAR(255))
BEGIN
select Question.Question_ID, Question.Question_Description
FROM Question, QuestionAllocator
WHERE Question.Question_ID=QuestionAllocator.QuestionID and Question.Question_ID=QID;
END$$
DELIMITER ;

Solution

  • You need to assign a value.

    CREATE PROCEDURE `QuestionTitle`(
        IN in_QID INT(11),
        OUT out_Description VARCHAR(255)
    )
    BEGIN
        SELECT out_Description := q.Question_Description
        FROM Question q
        WHERE q.Question_ID = in_QID;
    END$$
    

    Note the other changes:

    • The input arguments clearly show they are either in or out parameters. This helps avoid conflict with column names.
    • Only one table is needed, so the other is removed.
    • The output parameter is assigned a value.
    • You need to learn proper, explicit, standard JOIN syntax, even if this query does not require a JOIN.