← Back to MySQL Course | Chapter 15: Stored Procedures & Functions | Lesson 2 of 5

Procedure Parameters

IN Parameters

IN parameters are the default parameter type and let you pass values into a procedure for it to read and use, such as an ID used to filter which rows the procedure works on — the caller's original value is never modified.

Example: IN Parameters

sql
DELIMITER //
CREATE PROCEDURE GetUserById(IN user_id INT)
BEGIN
  SELECT * FROM users WHERE id = user_id;
END //
DELIMITER ;
CALL GetUserById(1);

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

OUT Parameters

OUT parameters flow in the opposite direction, letting a procedure hand a computed value — like a row count or a status code — back to whatever called it, without the caller needing to pass anything meaningful in first.

Example: OUT Parameters

sql
DELIMITER //
CREATE PROCEDURE CountUsers(OUT total INT)
BEGIN
  SELECT COUNT(*) INTO total FROM users;
END //
DELIMITER ;
CALL CountUsers(@total);
SELECT @total;

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

INOUT Parameters

INOUT parameters combine both directions: the caller passes in a starting value, the procedure can read and change it internally, and the updated value is handed back once the procedure finishes.

Example: INOUT Parameters

sql
DELIMITER //
CREATE PROCEDURE DoubleValue(INOUT n INT)
BEGIN
  SET n = n * 2;
END //
DELIMITER ;
SET @val = 5;
CALL DoubleValue(@val);
SELECT @val;

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

Mixing Parameter Types

A single procedure can mix IN, OUT, and INOUT parameters together freely, giving you fine control over exactly which values flow in, which flow out, and which do both.

Example: Mixing Parameter Types

sql
DELIMITER //
CREATE PROCEDURE AdjustStock(IN product_id INT, INOUT stock INT, OUT status TEXT)
BEGIN
  SET stock = stock - 1;
  SET status = IF(stock > 0, 'ok', 'out of stock');
END //
DELIMITER ;

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

Handling Missing Values in Parameters

Every parameter defined on a procedure must be supplied when it's called, whether as a literal value or a variable — leaving one out causes MySQL to raise an error rather than silently using a default.

Example: Handling Missing Values in Parameters

sql
DELIMITER //
CREATE PROCEDURE GetUserById(IN user_id INT)
BEGIN
  SELECT * FROM users WHERE id = user_id;
END //
DELIMITER ;
-- CALL GetUserById();  -- errors: missing required parameter
CALL GetUserById(1);

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

🔒

Chapter Quiz — Complete all 5 topics to unlock

0/5 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.