What Is a Stored Procedure?
A stored procedure is a named block of SQL saved in the database. You run it with CALL. It can take parameters, use variables and loops, and run many statements together. Teams use them to keep complex rules next to the data, or to cut round trips between app and database.
DELIMITER in the mysql Client
In the mysql command-line client the ; ends a statement, but a procedure body contains ;. So you temporarily change the delimiter:
DELIMITER is a client command, not SQL, so tools like Workbench or an application driver do not need it. The examples below are executed by a driver, so they show the body without it.
Your First Procedure
Result:
| name | price |
|---|---|
| Notebook | 3.50 |
| Pen Pack | 5.00 |
OUT and INOUT Parameters
Result:
| order_1_total |
|---|
| 1057.00 |
@t is a user variable. It lives for the connection and receives the OUT value.
Result:
| with_tax |
|---|
| 236.00 |
Variables, IF and CASE
Result:
| product_id | stock | status |
|---|---|---|
| 8 | 0 | OUT |
DECLARE creates a local variable and must come first inside BEGIN ... END. Local variables do not use @.
Loops
Result:
| n | sq |
|---|---|
| 1 | 1 |
| 2 | 4 |
| 3 | 9 |
| 4 | 16 |
| 5 | 25 |
REPEAT ... UNTIL and LOOP ... LEAVE are the other loop forms.
A Transaction Inside a Procedure
Result:
| result |
|---|
| ok |
Result:
| result |
|---|
| failed, rolled back |
Result:
| laptop_stock | order_100_rows |
|---|---|
| 13 | 1 |
The first call worked. The second used an unknown product, the foreign key failed, the handler rolled back and reported it. Handlers are explained in a later lesson.
Managing Procedures
Result:
| ROUTINE_NAME | ROUTINE_TYPE |
|---|---|
| p1 | PROCEDURE |
There is no CREATE OR REPLACE PROCEDURE in MySQL. Drop then create.
When to Use Procedures
| Good fit | Think twice |
|---|---|
| Batch jobs near the data | Complex business rules that need version control and tests (keep in the app) |
| Cutting many round trips | Logic that must run on several database products |
| Controlled data changes with permissions | Heavy computation (databases are hard to scale out) |
Common Beginner Mistakes
| Mistake | What happens | Fix |
|---|---|---|
| Parameter named like a column | Query compares the parameter to itself | Prefix parameters, for example p_cat |
Using @var for locals | Value leaks across calls | Use DECLARE |
Forgetting DELIMITER in the client | Syntax error at the first ; | Change the delimiter |
| No error handler around transactions | Half-done work | Handler with ROLLBACK |
Expecting CREATE OR REPLACE | Syntax error | DROP ... IF EXISTS first |
Practice
- Write a procedure that returns orders for a customer id.
- Add an OUT parameter with the customer's order count.
- Write a procedure that raises stock of all products below a given level.
- List all routines in your schema.
Conclusion
A stored procedure is saved SQL you CALL. Use IN, OUT and INOUT parameters, DECLARE locals, IF and loops, and wrap multi-step changes in a transaction with an error handler.

