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.
Practice
IN parameter type in a SQL stored procedure do?Solution
Step 1: Understand parameter types in SQL procedures
Parameters can beIN,OUT, orINOUT.INmeans input only.Step 2: Identify the role of
INparametersINparameters pass values into the procedure but do not return values.Final Answer:
It allows the procedure to receive input values only. -> Option BQuick Check:
IN= input only [OK]
- Confusing IN with OUT or INOUT
- Thinking IN parameters can return values
- Assuming IN declares internal variables
OUT parameter named result of type INT in a MySQL stored procedure?Solution
Step 1: Recall parameter declaration syntax in MySQL
Parameters are declared asIN,OUT, orINOUTfollowed by name and type.Step 2: Check each option's syntax
CREATE PROCEDURE proc_name(OUT result INT) uses correct syntax:OUT result INT. CREATE PROCEDURE proc_name(INOUT result INT) uses INOUT, not OUT. CREATE PROCEDURE proc_name(IN result INT) uses IN, not OUT. CREATE PROCEDURE proc_name(RESULT OUT INT) has wrong order and keyword.Final Answer:
CREATE PROCEDURE proc_name(OUT result INT) -> Option CQuick Check:
OUT parameter syntax = OUT name type [OK]
- Using IN instead of OUT
- Swapping parameter order
- Using wrong keywords like RESULT OUT
CALL calculate_sum(5, 10, @total);
SELECT @total;
Given the procedure signature:
CREATE PROCEDURE calculate_sum(IN a INT, IN b INT, OUT sum INT)
BEGIN
SET sum = a + b;
END;
What will be the output of
SELECT @total;?Solution
Step 1: Understand procedure parameters and call
The procedure takes two input numbers and outputs their sum via the OUT parametersum. The call passes 5 and 10 as inputs, and @total as the OUT variable.Step 2: Calculate the output value
The procedure setssum = 5 + 10 = 15. This value is assigned to the session variable@total.Final Answer:
15 -> Option DQuick Check:
5 + 10 = 15 [OK]
- Expecting NULL because of no SELECT inside procedure
- Confusing IN and OUT parameters
- Not using session variables for OUT
CREATE PROCEDURE update_value(INOUT val INT)
BEGIN
SET val = val + 1;
END;
Which of the following calls will correctly update the session variable
@x from 10 to 11?Solution
Step 1: Understand INOUT parameter usage
INOUT parameters require a variable to pass in and receive updated value. Session variables like@xcan be used.Step 2: Analyze each call
CALL update_value(@x); passes@xcorrectly. CALL update_value(10); passes a literal 10, which is not a variable. CALL update_value(INOUT @x); uses invalid syntax. CALL update_value(IN @x); uses IN keyword, which is incorrect for INOUT.Final Answer:
CALL update_value(@x); -> Option AQuick Check:
INOUT needs variable, not literal [OK]
- Passing literals instead of variables
- Using IN or INOUT keywords in CALL
- Expecting procedure to update literals
INOUT parameters a and b. Which of the following procedure bodies correctly swaps the values?Solution
Step 1: Understand swapping logic with INOUT parameters
Swapping requires a temporary variable to hold one value while assigning the other.Step 2: Evaluate each option's correctness
BEGIN DECLARE temp INT; SET temp = a; SET a = b; SET b = temp; END uses a temporary variable to swap correctly. BEGIN SET a = b; SET b = a; END overwritesabefore assigningb. BEGIN SET a = a + b; SET b = a - b; SET a = a - b; END uses arithmetic swap but is valid only if no overflow and no NULLs. BEGIN SET a = b; SET b = a + b; END does not swap correctly.Final Answer:
BEGIN DECLARE temp INT; SET temp = a; SET a = b; SET b = temp; END -> Option AQuick Check:
Temporary variable swap = correct [OK]
- Overwriting variables without temp
- Using arithmetic swap without checks
- Ignoring NULL or overflow risks
Ready to prove it?
Pass the test and this pattern moves unknown
