Day 87 – Stored Procedures
CREATE PROCEDURE spGetSalesByRegion (@RegionName NVARCHAR(50))
AS
BEGIN
SELECT SUM(SalesAmount) AS TotalSales, YEAR(SalesDate) AS SalesYear
FROM Sales
WHERE RegionName = @RegionName
GROUP BY YEAR(SalesDate)
END
EXEC spGetSalesByRegion 'North America'
In this example, a stored procedure named spGetSalesByRegion
is created to retrieve the total sales for a specified region and group the results by year. The procedure accepts a parameter @RegionName
which is used in the WHERE
clause to filter the data.