SQL · Pattern 13 — Database Objects
Stored Procedures
A stored procedure is a precompiled collection of SQL statements that can be executed repeatedly with input parameters.
Source data — Pattern 13: Database Objects
All queries run against the shared employees table (see schema.sql). This pattern introduces commonly used database objects including views, stored procedures, user-defined functions, and triggers. All examples are built using the existing employees table.


What it does
A stored procedure is a precompiled collection of SQL statements that can be executed repeatedly with input parameters.
Problem
Create a procedure to fetch employees by department.
CREATE PROCEDURE GetEmployeesByDepartment
@DeptId INT
AS
BEGIN
SELECT *
FROM employees
WHERE department_id = @DeptId;
END;
Result
