Stored Procedure

A stored procedure is a precompiled SQL program that is stored in the database and can be executed repeatedly by calling it. It helps in reusability, security, and efficiency by reducing the need for repetitive query execution.

Explanation:

A stored procedure consists of SQL statements that can be executed as a unit. It is beneficial for:

  • Performance: Since it’s precompiled, execution is faster.
  • Security: Users can execute procedures without directly accessing tables.
  • Reusability: Procedures can be called multiple times to avoid repetitive query writing.

Example SQL Stored Procedure:

sqlCopyEditDELIMITER //
CREATE PROCEDURE GetEmployeeSalary(IN emp_id INT)
BEGIN
    SELECT salary FROM Employees WHERE employee_id = emp_id;
END //
DELIMITER ;

To call the procedure:

sqlCopyEditCALL GetEmployeeSalary(101);