MySQL + SQL · Lesson 95

Stored Procedures in MySQL

What is a Stored Procedure?

A stored procedure is a saved block of SQL you can run by name, with parameters. It saves repeating the same logic.

Create and Call

DELIMITER //
CREATE PROCEDURE add_student(IN p_name VARCHAR(50), IN p_marks INT)
BEGIN
    INSERT INTO students(name, marks) VALUES(p_name, p_marks);
END //
DELIMITER ;

CALL add_student('Aman', 88);
CALL runs the saved procedure with the given values.

Why Use Them?

  • Reuse complex logic by name.
  • Reduce network traffic (one CALL vs many queries).
  • Centralise business rules in the database.

Summary

  • A stored procedure is saved SQL run with CALL.
  • Use IN parameters to pass values; great for reusable logic.
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

WhatsApp