I am working on a web project where I need to get (let them say) employee records. In some cases, I have to get one record by providing EmployeeID. In other cases, I should get a few employee records by providing the SectorID. This logic can be expanded to cover additional scenarios: get all employee records, get employee records for qualifications, etc.
Is it good to use a single stored procedure that accepts a variable number of parameters for processing different scripts (using default values if the parameter is not specified). Example:
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)
source share