LearnContact
Lesson 5520 min read

Stored Procedures

Learn how to create, call, and parameterize stored procedures — reusable blocks of SQL logic saved inside the database itself.

Introduction

Sometimes the same sequence of SQL statements needs to run over and over — validating input, inserting a row, then updating a related total. Instead of repeating that logic in every application that touches the database, MySQL lets you save it once, inside the database, as a stored procedure.

This lesson covers what a stored procedure is, how to define one using the DELIMITER command, how to call it, and how to pass values in and out of it.

What You Will Learn
  • What a stored procedure is and why it is useful.
  • Why the DELIMITER command is needed to define one.
  • How to create a procedure with CREATE PROCEDURE.
  • How to call a procedure with CALL.
  • The difference between IN and OUT parameters.

What is a Stored Procedure?

A stored procedure is a saved, reusable block of SQL logic stored inside the database itself. Once created, it can be executed — or "called" — by name, as many times as needed, optionally with different input values each time.

Simple Definition
  • A stored procedure bundles one or more SQL statements under a single name.
  • It lives inside the database, not inside your application code.
  • It is executed with the CALL keyword.
  • It can accept parameters, so the same procedure can behave differently each time it runs.

The DELIMITER Command

Normally, MySQL treats a semicolon (;) as the end of a statement. But a stored procedure's body is itself made up of multiple statements separated by semicolons — so if MySQL kept treating semicolons as "end of command," it would stop reading the procedure definition halfway through.

The DELIMITER command temporarily changes what character marks the end of a command in the MySQL command-line client, so the whole procedure definition can be sent as one block.

DELIMITER $$

CREATE PROCEDURE example_procedure()
BEGIN
    SELECT 'Hello from a stored procedure!';
END$$

DELIMITER ;
Why $$ ?

You can pick almost any symbol not otherwise used, like $$ or //. It simply marks "the procedure definition truly ends here," letting the semicolons inside BEGIN...END be interpreted normally as part of the procedure body. Once the definition is finished, DELIMITER ; restores normal behavior.

Creating a Stored Procedure

Consider a "students" table where new students are regularly added. Instead of writing a fresh INSERT statement every time, save the logic once as a procedure.

CREATE TABLE students (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(50),
    marks INT
);

DELIMITER $$

CREATE PROCEDURE add_student(
    IN student_name VARCHAR(50),
    IN student_marks INT
)
BEGIN
    INSERT INTO students (name, marks)
    VALUES (student_name, student_marks);
END$$

DELIMITER ;

This procedure, "add_student", takes a name and marks value as input and inserts a new row into "students" every time it is called.

Calling a Stored Procedure

A stored procedure is executed with the CALL keyword, followed by its name and any arguments it expects.

CALL add_student('Alex', 78);
CALL add_student('Priya', 92);

SELECT * FROM students;
Result
+----+-------+-------+
| id | name  | marks |
+----+-------+-------+
| 1  | Alex  | 78    |
| 2  | Priya | 92    |
+----+-------+-------+

Every call runs the exact same saved logic with new input values, avoiding repeated, hand-written INSERT statements throughout the application.

IN and OUT Parameters

Stored procedure parameters can be marked IN, OUT, or INOUT.

Parameter TypeMeaning
INA value passed into the procedure (the default). The procedure cannot change the caller's original value.
OUTA value the procedure sets, which is passed back out to the caller after it finishes.
INOUTA value passed in and potentially modified, then passed back out again.

Here is a procedure that uses an OUT parameter to report back how many students currently have marks of 60 or above.

DELIMITER $$

CREATE PROCEDURE count_passing_students(
    OUT passing_count INT
)
BEGIN
    SELECT COUNT(*) INTO passing_count
    FROM students
    WHERE marks >= 60;
END$$

DELIMITER ;
CALL count_passing_students(@result);
SELECT @result AS passing_students;
Result
+-------------------+
| passing_students  |
+-------------------+
| 2                 |
+-------------------+

The @result variable is a user session variable that receives the OUT parameter's value once the procedure finishes running.

Dropping a Procedure

A stored procedure that is no longer needed can be removed with DROP PROCEDURE.

DROP PROCEDURE add_student;

Common Mistakes

Avoid These Mistakes
  • Forgetting to change DELIMITER before defining a procedure, causing MySQL to stop at the first semicolon inside the body.
  • Forgetting to switch DELIMITER back to ; after the procedure is defined.
  • Trying to use a stored procedure directly inside a SELECT statement — procedures are called with CALL, not used as expressions.
  • Confusing IN and OUT parameters, and expecting an IN parameter to pass a modified value back to the caller.

Best Practices

  • Use stored procedures for logic that is repeated often, especially multi-step operations.
  • Give procedures clear, action-based names, like add_student or count_passing_students.
  • Keep procedure bodies focused on one clear task rather than combining many unrelated actions.
  • Always restore DELIMITER ; immediately after defining a procedure.

Frequently Asked Questions

Is DELIMITER part of standard SQL?

No, it is a command specific to MySQL client tools, used only to tell the client where a multi-statement definition ends. It is not itself sent to the MySQL server as SQL.

Can a stored procedure return a value like a function?

Not directly as an expression result — a procedure can produce result sets (via SELECT) or use OUT parameters, but it cannot be used inline inside another SELECT expression the way a function can.

Can a stored procedure call another stored procedure?

Yes, one procedure can call another from within its body, which helps break complex logic into smaller, reusable pieces.

Do stored procedures run faster than sending the same SQL from an application?

They can, since the SQL is already parsed and stored inside the database and avoids sending multiple separate statements back and forth over the network.

Key Takeaways

  • A stored procedure is a saved, reusable block of SQL logic stored inside the database.
  • DELIMITER changes the statement-ending character so a multi-statement procedure body can be defined safely.
  • CREATE PROCEDURE name(...) BEGIN ... END defines a procedure.
  • CALL procedure_name(...) executes it.
  • IN parameters pass values in; OUT parameters pass values back out to the caller.

Summary

Stored procedures let you save reusable SQL logic directly inside the database, callable by name with CALL, and configurable through IN, OUT, and INOUT parameters.

In this lesson, you learned what a stored procedure is, why DELIMITER is needed to define one, how to call a procedure, and the difference between IN and OUT parameters. Next, you will learn about user-defined functions — a close relative of stored procedures that can be used directly inside expressions.

Lesson 55 Completed
  • You understand what a stored procedure is.
  • You know why DELIMITER is required to define one.
  • You can create and call a procedure with parameters.
  • You are ready to learn about user-defined functions.
Next Lesson →

Functions