-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassignment_2.sql
225 lines (187 loc) · 5.67 KB
/
assignment_2.sql
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
CREATE PROCEDURE InsertOrderDetails
@OrderID INT,
@ProductID INT,
@UnitPrice DECIMAL(18, 2) = NULL,
@Quantity INT,
@Discount DECIMAL(18, 2) = 0
AS
BEGIN
DECLARE @ProductUnitPrice DECIMAL(18, 2);
DECLARE @UnitsInStock INT;
DECLARE @ReorderLevel INT;
-- Fetch UnitPrice and UnitsInStock from Product table if UnitPrice is not provided
SELECT @ProductUnitPrice = UnitPrice, @UnitsInStock = UnitsInStock, @ReorderLevel = ReorderLevel
FROM Products
WHERE ProductID = @ProductID;
-- Use Product UnitPrice if not provided
SET @UnitPrice = ISNULL(@UnitPrice, @ProductUnitPrice);
-- Check if sufficient stock exists
IF @UnitsInStock >= @Quantity
BEGIN
INSERT INTO OrderDetails (OrderID, ProductID, UnitPrice, Quantity, Discount)
VALUES (@OrderID, @ProductID, @UnitPrice, @Quantity, @Discount);
IF @@ROWCOUNT = 0
BEGIN
PRINT 'Failed to place the order. Please try again.';
RETURN;
END
-- Update stock
UPDATE Products
SET UnitsInStock = UnitsInStock - @Quantity
WHERE ProductID = @ProductID;
-- Check if stock falls below reorder level
IF @UnitsInStock - @Quantity < @ReorderLevel
BEGIN
PRINT 'Warning: Quantity in stock has dropped below its Reorder Level.';
END
END
ELSE
BEGIN
PRINT 'Insufficient stock to fill the order. Order aborted.';
END
END;
CREATE PROCEDURE UpdateOrderDetails
@OrderID INT,
@ProductID INT,
@UnitPrice DECIMAL(18, 2) = NULL,
@Quantity INT = NULL,
@Discount DECIMAL(18, 2) = NULL
AS
BEGIN
DECLARE @CurrentUnitPrice DECIMAL(18, 2);
DECLARE @CurrentQuantity INT;
DECLARE @CurrentDiscount DECIMAL(18, 2);
-- Fetch current values from OrderDetails
SELECT @CurrentUnitPrice = UnitPrice, @CurrentQuantity = Quantity, @CurrentDiscount = Discount
FROM OrderDetails
WHERE OrderID = @OrderID AND ProductID = @ProductID;
-- Use current values if new values are NULL
SET @UnitPrice = ISNULL(@UnitPrice, @CurrentUnitPrice);
SET @Quantity = ISNULL(@Quantity, @CurrentQuantity);
SET @Discount = ISNULL(@Discount, @CurrentDiscount);
-- Update OrderDetails
UPDATE OrderDetails
SET UnitPrice = @UnitPrice, Quantity = @Quantity, Discount = @Discount
WHERE OrderID = @OrderID AND ProductID = @ProductID;
-- Adjust stock in Products table
DECLARE @StockChange INT;
SET @StockChange = @CurrentQuantity - @Quantity;
UPDATE Products
SET UnitsInStock = UnitsInStock + @StockChange
WHERE ProductID = @ProductID;
END;
CREATE PROCEDURE GetOrderDetails
@OrderID INT
AS
BEGIN
IF NOT EXISTS (SELECT * FROM OrderDetails WHERE OrderID = @OrderID)
BEGIN
PRINT 'The OrderID ' + CAST(@OrderID AS VARCHAR) + ' does not exist';
RETURN 1;
END
SELECT * FROM OrderDetails WHERE OrderID = @OrderID;
END;
CREATE PROCEDURE DeleteOrderDetails
@OrderID INT,
@ProductID INT
AS
BEGIN
IF NOT EXISTS (SELECT * FROM OrderDetails WHERE OrderID = @OrderID AND ProductID = @ProductID)
BEGIN
PRINT 'Invalid parameters: The given OrderID or ProductID does not exist in the Order Details table';
RETURN -1;
END
DELETE FROM OrderDetails WHERE OrderID = @OrderID AND ProductID = @ProductID;
END;
CREATE FUNCTION FormatDateMMDDYYYY (@InputDate DATETIME)
RETURNS VARCHAR(10)
AS
BEGIN
RETURN CONVERT(VARCHAR(10), @InputDate, 101);
END;
CREATE VIEW vwCustomerOrders
AS
SELECT
c.CompanyName,
o.OrderID,
o.OrderDate,
od.ProductID,
p.ProductName,
od.Quantity,
od.UnitPrice,
(od.Quantity * od.UnitPrice) AS TotalPrice
FROM
Orders o
JOIN
Customers c ON o.CustomerID = c.CustomerID
JOIN
OrderDetails od ON o.OrderID = od.OrderID
JOIN
Products p ON od.ProductID = p.ProductID;
CREATE VIEW vwCustomerOrdersYesterday
AS
SELECT
c.CompanyName,
o.OrderID,
o.OrderDate,
od.ProductID,
p.ProductName,
od.Quantity,
od.UnitPrice,
(od.Quantity * od.UnitPrice) AS TotalPrice
FROM
Orders o
JOIN
Customers c ON o.CustomerID = c.CustomerID
JOIN
OrderDetails od ON o.OrderID = od.OrderID
JOIN
Products p ON od.ProductID = p.ProductID
WHERE
CAST(o.OrderDate AS DATE) = CAST(DATEADD(DAY, -1, GETDATE()) AS DATE);
CREATE VIEW MyProducts
AS
SELECT
p.ProductID,
p.ProductName,
p.QuantityPerUnit,
p.UnitPrice,
s.CompanyName,
c.CategoryName
FROM
Products p
JOIN
Suppliers s ON p.SupplierID = s.SupplierID
JOIN
Categories c ON p.CategoryID = c.CategoryID
WHERE
p.Discontinued = 0;
CREATE TRIGGER trgInsteadOfDeleteOrders
ON Orders
INSTEAD OF DELETE
AS
BEGIN
DELETE FROM OrderDetails WHERE OrderID IN (SELECT OrderID FROM DELETED);
DELETE FROM Orders WHERE OrderID IN (SELECT OrderID FROM DELETED);
END;
CREATE TRIGGER trgCheckStockBeforeInsert
ON OrderDetails
INSTEAD OF INSERT
AS
BEGIN
DECLARE @ProductID INT, @Quantity INT, @UnitsInStock INT;
SELECT @ProductID = ProductID, @Quantity = Quantity FROM INSERTED;
SELECT @UnitsInStock = UnitsInStock FROM Products WHERE ProductID = @ProductID;
IF @UnitsInStock >= @Quantity
BEGIN
INSERT INTO OrderDetails
SELECT * FROM INSERTED;
UPDATE Products
SET UnitsInStock = UnitsInStock - @Quantity
WHERE ProductID = @ProductID;
END
ELSE
BEGIN
PRINT 'Insufficient stock to fill the order. Order not inserted.';
END
END;