๐Ÿงพ MySQL Stored Procedures โ€“ Define, Call, and Manage Reusable SQL Logic


๐Ÿงฒ Introduction โ€“ Why Use Stored Procedures in MySQL?

Stored procedures are named blocks of SQL logic saved on the MySQL server that can be executed repeatedly using a single CALL statement. They help:

  • ๐Ÿ” Reduce duplication of code
  • ๐Ÿ”’ Centralize logic in the database
  • ๐Ÿš€ Improve performance by reducing client-server communication
  • ๐Ÿ“ฆ Package multi-step operations (like validations, inserts, and updates)

๐ŸŽฏ In this guide, youโ€™ll learn:

  • What stored procedures are and how to define them
  • Syntax for creating, calling, and dropping procedures
  • Input/output parameter types
  • Best practices and real-world use cases

๐Ÿงฑ 1. What Is a Stored Procedure?

A Stored Procedure is a set of SQL statements with a name, stored in the database, and executed with a CALL command.

  • Supports input (IN), output (OUT), and input/output (INOUT) parameters
  • Doesnโ€™t return a value (unlike functions), but can return data via parameters or result sets

๐Ÿ› ๏ธ 2. Creating a Stored Procedure

๐Ÿ”น Basic Syntax

DELIMITER //

CREATE PROCEDURE procedure_name ( [parameters] )
BEGIN
  -- SQL statements
END //

DELIMITER ;

๐Ÿ”น Example โ€“ Insert New Customer

DELIMITER //

CREATE PROCEDURE add_customer(
  IN customer_name VARCHAR(100),
  IN customer_email VARCHAR(100)
)
BEGIN
  INSERT INTO customers(name, email)
  VALUES (customer_name, customer_email);
END //

DELIMITER ;

๐Ÿ“ž 3. Calling a Stored Procedure

CALL add_customer('Alice', 'alice@example.com');

โœ… Executes the add_customer procedure with provided parameters.


๐Ÿ”ƒ 4. IN, OUT, INOUT Parameters

๐Ÿ”น IN (Input Only)

IN username VARCHAR(50)

โœ… Used to pass values into the procedure.


๐Ÿ”น OUT (Output Only)

DELIMITER //

CREATE PROCEDURE get_customer_count(OUT count INT)
BEGIN
  SELECT COUNT(*) INTO count FROM customers;
END //

DELIMITER ;

๐Ÿ”น Call & Capture Output

CALL get_customer_count(@total);
SELECT @total;

๐Ÿ”น INOUT (Input and Modified Output)

INOUT price DECIMAL(10,2)

โœ… Passed in, possibly changed inside the procedure, and returned.


๐Ÿ” 5. Show Stored Procedures

SHOW PROCEDURE STATUS WHERE Db = 'your_database';

โŒ 6. Drop Stored Procedure

DROP PROCEDURE IF EXISTS add_customer;

๐Ÿ” 7. Grant Execute Privileges

GRANT EXECUTE ON PROCEDURE add_customer TO 'app_user'@'localhost';

โœ… Controls who can execute the procedure.


๐Ÿš€ 8. Real-World Use Cases

ScenarioProcedure NamePurpose
Insert a new usercreate_userAccepts name, email, inserts into table
Get monthly salessales_summaryReturns aggregated totals for a month
Update order statusupdate_order_statusChanges status and logs update
Customer info + ordersget_customer_infoReturns customer details and recent orders

๐Ÿ“˜ Best Practices

โœ… Tip๐Ÿ’ก Why It Matters
Use DELIMITER // when defining routinesPrevents conflicts with semicolon in body
Keep logic focusedImproves reusability and readability
Use IN, OUT, INOUT correctlyMakes parameters intuitive
Document parameters clearlyHelps future developers understand usage
Avoid business logic that belongs in appKeep procedures data-centric

๐Ÿ“Œ Summary โ€“ Recap & Next Steps

MySQL Stored Procedures allow you to package complex operations and run them efficiently with a single command. Theyโ€™re perfect for automation, reusability, and centralizing multi-step logic.

๐Ÿ” Key Takeaways

  • Use CREATE PROCEDURE with IN, OUT, or INOUT parameters
  • Call procedures using CALL proc_name(args)
  • Store and reuse common database operations
  • Use SHOW PROCEDURE STATUS to list available procedures
  • Secure execution with GRANT EXECUTE

โš™๏ธ Real-World Relevance

Commonly used in API backends, automation scripts, reporting pipelines, and transactional workflows like e-commerce and CRMs.


โ“ FAQ โ€“ MySQL Stored Procedures


โ“ Whatโ€™s the difference between a stored procedure and a stored function?

  • Procedure: Executed with CALL, may return result sets
  • Function: Returns a single value and can be used in SQL expressions

โ“ Can I return a result set from a procedure?

โœ… Yes. Just use SELECT inside the procedure body.


โ“ Can a procedure call another procedure?

โœ… Yes. You can nest calls between stored routines.


โ“ How do I change a stored procedure?

You must DROP and then CREATE againโ€”MySQL doesnโ€™t support ALTER PROCEDURE.


โ“ Can stored procedures use transactions?

โœ… Yes. You can use START TRANSACTION, COMMIT, and ROLLBACK within procedures.


Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

๐Ÿงพ MySQL Stored Procedures

Or Copy Link

CONTENTS
Scroll to Top