-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2.1 Stored Procedures
121 lines (102 loc) · 2.49 KB
/
2.1 Stored Procedures
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
--Retrieve all orders for a specific customer
CREATE PROCEDURE GetOrdersByCustomer
@CustomerID INT
AS
BEGIN
SELECT
o.orderid,
o.orderdate,
c.customername,
p.name AS plantname,
o.quantity
FROM
Orders o
INNER JOIN
Customers c ON o.customerid = c.customerid
INNER JOIN
Plants p ON o.plantid = p.plantid
WHERE
c.customerid = @CustomerID;
END;
EXEC GetOrdersByCustomer @CustomerID = 1;
--Procedure to update the quantity of a plant in the Plants table
CREATE PROCEDURE UpdatePlantQuantity
@PlantID INT,
@NewQuantity INT
AS
BEGIN
UPDATE Plants
SET quantity = @NewQuantity
WHERE plantid = @PlantID;
END;
EXEC UpdatePlantQuantity @PlantID = 1, @NewQuantity = 50;
EXEC UpdatePlantQuantity @PlantID = 2, @NewQuantity = 30;
EXEC UpdatePlantQuantity @PlantID = 5, @NewQuantity = 20;
--Procedure to get plants by a price range
CREATE PROCEDURE GetPlantsByPriceRange
@MinPrice DECIMAL(10, 2),
@MaxPrice DECIMAL(10, 2)
AS
BEGIN
SELECT
plantid,
name,
species,
origincountry,
price,
sunlightdemand,
size,
quantity
FROM
Plants
WHERE
price BETWEEN @MinPrice AND @MaxPrice;
END;
EXEC GetPlantsByPriceRange @minprice=5, @maxprice=8
-- Procedure to delete a customer
CREATE PROCEDURE DeleteCustomer
@CustomerID INT
AS
BEGIN
BEGIN TRANSACTION;
DELETE FROM Orders
WHERE customerid = @CustomerID;
DELETE FROM Customers
WHERE customerid = @CustomerID;
COMMIT;
END;
EXEC DeleteCustomer @CUSTOMERID=10
--Updates prices from origin country for the plants
CREATE PROCEDURE UpdatePlantPriceByCountry
@Country VARCHAR(100),
@PercentageIncrease DECIMAL(5, 2)
AS
BEGIN
UPDATE Plants
SET Price = Price * (1 + (@PercentageIncrease / 100)
WHERE OriginCountry = @Country;
END;
EXEC UpdatePlantPriceByCountry @Country = 'USA', @PercentageIncrease = 10.00;
EXEC UpdatePlantPriceByCountry @Country = 'China', @PercentageIncrease = 5.00;
EXEC UpdatePlantPriceByCountry @Country = 'France', @PercentageIncrease = 7.00;
EXEC UpdatePlantPriceByCountry @Country = 'Various', @PercentageIncrease = 2.00;
--Retrieves top 5 best selling plants
CREATE PROCEDURE GetTopSellingPlants
AS
BEGIN
SELECT TOP 5
P.PlantID,
P.Name AS PlantName,
P.Species,
SUM(O.Quantity) AS TotalQuantitySold
FROM
Plants P
JOIN Orders O ON P.PlantID = O.PlantID
GROUP BY
P.PlantID,
P.Name,
P.Species
ORDER BY
TotalQuantitySold DESC;
END;
EXEC GetTopSellingPlants;