RienSQL
LESSON 59 / 62 · Stored procedures

Stored procedures

Creating and using stored procedures in SQL. Syntax, parameters, conditional logic, loops, and practical examples.

Stored Procedures in SQL

Stored procedures are program blocks that execute a specific sequence of actions in a database.

Unlike functions, procedures can modify data, perform complex business logic, and don't necessarily return a value.

General Structure of a Stored Procedure

CREATE PROCEDURE procedure_name([[IN | OUT | INOUT] parameter type [, ...]])
BEGIN
    -- procedure logic
END;
  • [ ]optional part
  • a | bone of the options
  • ...can be repeated

The parameter's direction: into the procedure, out of it, or both ways. IN is the default; see below for details.

The parameter name, used to access its value inside the procedure. You can list several parameters separated by commas, or leave the parentheses empty if you need none.

procedure_name
The name you use to call the procedure with CALL.
IN | OUT | INOUT
KeywordOptional
parameter
Optional
type
The parameter's data type: INT, DATE, VARCHAR(50), and so on.

Simple Procedure Example

Let's create a procedure to update student information:

-- Create procedure
CREATE PROCEDURE update_student_info(
    IN student_id INT,
    IN new_first_name VARCHAR(50),
    IN new_last_name VARCHAR(50)
)
BEGIN
    UPDATE Student
    SET first_name = new_first_name,
        last_name = new_last_name
    WHERE id = student_id;
END;

-- Call procedure
CALL update_student_info(1, 'Alexander', 'Smirnov');

-- Check the result
SELECT * FROM Student WHERE id = 1;
idfirst_namemiddle_namelast_namebirthdayaddress
1AlexanderFedorovichSmirnov2000-10-01T00:00:00.000Zul. Pushkina, d. 36, kv. 5

This procedure takes a student ID and new data, then updates the corresponding record in the Student table.

Types of Procedure Parameters

MySQL procedures support three types of parameters that can be passed to a stored procedure:

  • IN — input parameters (default)
  • OUT — output parameters for returning values
  • INOUT — parameters that can be both input and output

Input Parameters (IN)

Input parameters pass data into the procedure. This is the most common type of parameter:

CREATE PROCEDURE add_subject(
    IN subject_id INT,
    IN subject_name VARCHAR(100)
)
BEGIN
    INSERT INTO Subject (id, name)
    VALUES (subject_id, subject_name);
END;

-- Call with input parameters
CALL add_subject(15, 'Mathematics');

Output Parameters (OUT)

Output parameters allow procedures to return values:

CREATE PROCEDURE get_student_info(
    IN student_id INT,
    OUT student_name VARCHAR(100),
    OUT student_age INT
)
BEGIN
    SELECT
        CONCAT(first_name, ' ', last_name),
        TIMESTAMPDIFF(YEAR, birthday, CURDATE())
    INTO student_name, student_age
    FROM Student
    WHERE id = student_id;
END;

-- Call procedure with output parameters
CALL get_student_info(1, @name, @age);
SELECT @name AS student_name, @age AS student_age;
student_namestudent_age
Nikolaj Sokolov24

Input-Output Parameters (INOUT)

INOUT parameters can accept a value and return a modified value:

CREATE PROCEDURE calculate_discount(
    INOUT price DECIMAL(10,2),
    IN discount_percent INT
)
BEGIN
    SET price = price - (price * discount_percent / 100);
END;

-- Using INOUT parameter
SET @original_price = 1000.00;
CALL calculate_discount(@original_price, 15);
SELECT @original_price AS discounted_price;
discounted_price
850

Example of Three Parameter Types

Examples of parameter usage in stored procedures
Examples of parameter usage in stored procedures

Key Differences Between Parameter Types

Parameter TypeDirectionUsage
INIncomingPass data to procedure
OUTOutgoingReturn result from procedure
INOUTBidirectionalModify passed value

Important: OUT and INOUT parameters in MySQL require using session variables (e.g., @variable_name) when calling the procedure.

Managing Stored Procedures

Viewing Existing Procedures

SHOW PROCEDURE STATUS WHERE Db = 'your_database_name';

Dropping a Procedure

DROP PROCEDURE IF EXISTS add_student;

Modifying a Procedure

To modify a procedure in MySQL, you need to drop the old version first, then create a new one:

DROP PROCEDURE IF EXISTS add_student;
-- Create new version of the procedure
CREATE PROCEDURE add_student(...) ...

Stored procedures are a powerful tool for implementing complex business logic directly in the database. They ensure logic centralization, improve performance, and guarantee data integrity! 🚀

Practice

Solve it in the editor. The query runs against a sample database in your browser.

Task 1Moving lessons
Create a procedure move_lessons with the parameters old_classroom and new_classroom of type INT that moves all lessons from the classroom old_classroom to the classroom new_classroom.
SQL editor
SQLite
Ctrl / ⌘ + ↵
Result

Write a query and run it to see the results.