⬅ Previous Topic
SQL NULL Handling – IS NULL, COALESCE, IFNULLNext Topic ⮕
SQL User Defined Functions (UDF)⬅ Previous Topic
SQL NULL Handling – IS NULL, COALESCE, IFNULLNext Topic ⮕
SQL User Defined Functions (UDF)Have you ever written the same SQL logic again and again — maybe to update marks or calculate average scores? What if you could save that logic once and call it whenever needed? That’s what Stored Procedures are for. They help you wrap SQL logic into reusable, callable programs inside the database.
A Stored Procedure is a saved block of SQL statements that performs a specific task. You can pass parameters to it, reuse it across systems, and avoid repeating logic in multiple queries or apps.
DELIMITER $$
CREATE PROCEDURE procedure_name (parameters)
BEGIN
-- SQL statements
END$$
DELIMITER ;
CREATE TABLE students (
roll_no INT PRIMARY KEY,
name VARCHAR(50),
class VARCHAR(10)
);
CREATE TABLE results (
roll_no INT,
subject VARCHAR(30),
marks INT
);
DELIMITER $$
CREATE PROCEDURE add_student (
IN p_roll_no INT,
IN p_name VARCHAR(50),
IN p_class VARCHAR(10)
)
BEGIN
INSERT INTO students (roll_no, name, class)
VALUES (p_roll_no, p_name, p_class);
END$$
DELIMITER ;
CALL add_student(6, 'Ravi Kumar', '9A');
DELIMITER $$
CREATE PROCEDURE get_student_marks(IN p_roll_no INT)
BEGIN
SELECT s.name, r.subject, r.marks
FROM students s
JOIN results r ON s.roll_no = r.roll_no
WHERE s.roll_no = p_roll_no;
END$$
DELIMITER ;
CALL get_student_marks(1);
name | subject | marks
----------------+---------+-------
Aarav Sharma | Maths | 85
Aarav Sharma | Science | 88
This procedure updates a student's marks only if the new value is higher than the current one.
DELIMITER $$
CREATE PROCEDURE update_marks_if_higher (
IN p_roll_no INT,
IN p_subject VARCHAR(30),
IN p_new_marks INT
)
BEGIN
DECLARE current_marks INT;
SELECT marks INTO current_marks
FROM results
WHERE roll_no = p_roll_no AND subject = p_subject;
IF current_marks IS NULL THEN
INSERT INTO results VALUES (p_roll_no, p_subject, p_new_marks);
ELSEIF p_new_marks > current_marks THEN
UPDATE results
SET marks = p_new_marks
WHERE roll_no = p_roll_no AND subject = p_subject;
END IF;
END$$
DELIMITER ;
SHOW PROCEDURE STATUS WHERE Db = 'your_database';
DROP PROCEDURE IF EXISTS add_student;
add_student
get_student_marks
Stored Procedures are like reusable functions that live inside your database. For school systems or any application with repeated operations — inserting, updating, querying — stored procedures simplify your life by combining logic and performance in one place.
Up next, we’ll explore Triggers — special procedures that run automatically when a row is inserted, updated, or deleted.
CREATE PROCEDURE GetStudentMarks(IN roll INT)
BEGIN
SELECT name, marks FROM students WHERE roll_no = roll;
END;
⬅ Previous Topic
SQL NULL Handling – IS NULL, COALESCE, IFNULLNext Topic ⮕
SQL User Defined Functions (UDF)You can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.