I am working on a web project where I have to retrieve (let's say) employee records. In some cases I have to retrieve a single record by providing an EmployeeID. In other cases, I have to retrieve multiple employee records by providing a SectorID. This logic could be expanded to cover additional scenarios: get all employee records, get employee records by qualification, etc.
Is it good practice to use one single stored procedure that accepts a variable number of parameters to handle different scenarios (using default values when the parameter is not provided). Ex:
CREATE PROCEDURE [dbo].[GetEmployeeRecords]
(
@employeeID int = -1,
@sectorID int = -1
)
AS
BEGIN
SELECT EmployeeID,
EmployeeFirstName,
EmployeeLastName,
s.SectorName
FROM dbo.Employees e
INNER JOIN Sectors s ON e.SectorID = s.SectorID
WHERE (e.EmployeeID = @EmployeeID OR @EmployeeID = -1)
AND (e.SectorID = @SectorID OR @SectorID = -1)