HomeMYSQLIN, OUT, and INOUT parameters mean in Stored Procedures (MySQL)

IN, OUT, and INOUT parameters mean in Stored Procedures (MySQL)

🔹 1. IN Parameter

  • Definition: Input only. You pass a value to the procedure, and it cannot be changed inside the procedure.
  • Default type if not mentioned.

Example:

CREATE PROCEDURE GetStudent(IN student_id INT)
BEGIN
   SELECT name, age FROM students WHERE id = student_id;
END;

CALL GetStudent(1);  -- Passes 1 as input

👉 The procedure receives a value (1), but cannot return through the same variable.


🔹 2. OUT Parameter

  • Definition: Output only. Procedure assigns a value to it and returns it to the caller.
  • You cannot pass a value into it.

Example:

CREATE PROCEDURE GetStudentName(IN student_id INT, OUT student_name VARCHAR(50))
BEGIN
   SELECT name INTO student_name FROM students WHERE id = student_id;
END;

CALL GetStudentName(1, @name);
SELECT @name;   -- Shows the output

👉 Here, @name receives the output from the procedure.


🔹 3. INOUT Parameter

  • Definition: Works as both input and output.
  • You pass a value into it, and the procedure can modify and return it.

Example:

CREATE PROCEDURE IncreaseMarks(INOUT marks INT)
BEGIN
   SET marks = marks + 10;
END;

SET @score = 50;
CALL IncreaseMarks(@score);
SELECT @score;   -- Returns 60

👉 The same variable is used to send input and get modified output.


✅ Quick Summary

ParameterDirectionCan Pass Value?Can Return Value?
INInput to procedure✅ Yes❌ No
OUTOutput from procedure❌ No✅ Yes
INOUTInput & Output✅ Yes✅ Yes

Share: 

No comments yet! You be the first to comment.

Leave a Reply

Your email address will not be published. Required fields are marked *