Parameters let you send and receive values when running a stored procedure. They help you share information between your code and the database.
Parameters (IN, OUT, INOUT) in SQL
CREATE PROCEDURE procedure_name ( IN param1 datatype, OUT param2 datatype, INOUT param3 datatype ) BEGIN -- procedure body END;
IN means the parameter is for input only.
OUT means the parameter is for output only.
INOUT means the parameter can be used for both input and output.
CREATE PROCEDURE AddNumbers( IN num1 INT, IN num2 INT, OUT sum INT ) BEGIN SET sum = num1 + num2; END;
CREATE PROCEDURE UpdateAndReturn( INOUT value INT ) BEGIN SET value = value + 10; END;
This procedure calculates the area of a rectangle using width and height as input, and returns the area as output.
We call it with width=5 and height=4, then select the output variable to see the result.
DELIMITER $$ CREATE PROCEDURE CalculateArea( IN width INT, IN height INT, OUT area INT ) BEGIN SET area = width * height; END$$ DELIMITER ; -- Call the procedure CALL CalculateArea(5, 4, @result); SELECT @result AS Area;
You must use session variables (like @result) to get OUT or INOUT values after calling a procedure.
IN parameters cannot be changed inside the procedure.
INOUT parameters start with a value and can be changed inside the procedure.
Parameters let procedures receive inputs and send outputs.
IN is input only, OUT is output only, INOUT is both.
Use session variables to capture OUT and INOUT results after calling procedures.