SQL
--create
CREATE PROCEDURE sp_test1
AS
BEGIN
select
C.Name as 'Category Name',
SUM(ISNULL(OL.Quantity,0)) as 'Ordered Quantity',
case
when SUM(ISNULL(OL.Quantity,0)) between 1 and 9 then 'Low-demand'
when SUM(ISNULL(OL.Quantity,0)) between 10 and 30 then 'Mid-demand'
when SUM(ISNULL(OL.Quantity,0)) > 30 then 'High-demand'
else 'No-demand'
end as 'Demand Type'
from Categories C
left join Products P on C.CategoryId = P.CategoryId
left join OrderLines OL on P.ProductId = OL.ProductId
group by C.Name
order by C.Name
END
EXEC sp_test1
--alter
ALTER PROCEDURE sp_test1
AS
BEGIN
select
C.Name as 'Category Name 1',
SUM(ISNULL(OL.Quantity,0)) as 'Ordered Quantity',
case
when SUM(ISNULL(OL.Quantity,0)) between 1 and 9 then 'Low-demand'
when SUM(ISNULL(OL.Quantity,0)) between 10 and 30 then 'Mid-demand'
when SUM(ISNULL(OL.Quantity,0)) > 30 then 'High-demand'
else 'No-demand'
end as 'Demand Type'
from Categories C
left join Products P on C.CategoryId = P.CategoryId
left join OrderLines OL on P.ProductId = OL.ProductId
group by C.Name
order by C.Name
END
EXEC sp_test1
DROP PROCEDURE sp_test1
--paramaters
CREATE PROCEDURE sp_test1
@categoryName as nvarchar(500)
AS
BEGIN
select
C.Name as 'Category Name',
SUM(ISNULL(OL.Quantity,0)) as 'Ordered Quantity',
case
when SUM(ISNULL(OL.Quantity,0)) between 1 and 9 then 'Low-demand'
when SUM(ISNULL(OL.Quantity,0)) between 10 and 30 then 'Mid-demand'
when SUM(ISNULL(OL.Quantity,0)) > 30 then 'High-demand'
else 'No-demand'
end as 'Demand Type'
from Categories C
left join Products P on C.CategoryId = P.CategoryId
left join OrderLines OL on P.ProductId = OL.ProductId
where c.Name = @categoryName
group by C.Name
order by C.Name
END
EXEC sp_test1 'fruit'
ALTER PROCEDURE sp_test1
@categoryName as nvarchar(500)
AS
BEGIN
SET NOCOUNT ON
select
C.Name as 'Category Name',
SUM(ISNULL(OL.Quantity,0)) as 'Ordered Quantity',
case
when SUM(ISNULL(OL.Quantity,0)) between 1 and 9 then 'Low-demand'
when SUM(ISNULL(OL.Quantity,0)) between 10 and 30 then 'Mid-demand'
when SUM(ISNULL(OL.Quantity,0)) > 30 then 'High-demand'
else 'No-demand'
end as 'Demand Type'
from Categories C
left join Products P on C.CategoryId = P.CategoryId
left join OrderLines OL on P.ProductId = OL.ProductId
where c.Name = @categoryName
group by C.Name
order by C.Name
END
EXEC sp_test1 'fruit'