-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdventureWorks_LevelB_Task.sql
More file actions
291 lines (249 loc) · 8.56 KB
/
AdventureWorks_LevelB_Task.sql
File metadata and controls
291 lines (249 loc) · 8.56 KB
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
-- ============================================
-- DATABASE: AdventureWorks2022
-- SQL OBJECTS: Procedures, Functions, Views, Triggers
-- ============================================
USE AdventureWorks2022;
GO
-- ============================================
-- FUNCTION: Format Date as MM/DD/YYYY
-- ============================================
CREATE FUNCTION dbo.fn_FormatDate_MMDDYYYY (@InputDate DATETIME)
RETURNS VARCHAR(10)
AS
BEGIN
RETURN RIGHT('0' + CAST(MONTH(@InputDate) AS VARCHAR), 2) + '/' +
RIGHT('0' + CAST(DAY(@InputDate) AS VARCHAR), 2) + '/' +
CAST(YEAR(@InputDate) AS VARCHAR)
END;
GO
-- ============================================
-- FUNCTION: Format Date as YYYYMMDD
-- ============================================
CREATE FUNCTION dbo.fn_FormatDate_YYYYMMDD (@InputDate DATETIME)
RETURNS VARCHAR(8)
AS
BEGIN
RETURN CAST(YEAR(@InputDate) AS VARCHAR) +
RIGHT('0' + CAST(MONTH(@InputDate) AS VARCHAR), 2) +
RIGHT('0' + CAST(DAY(@InputDate) AS VARCHAR), 2)
END;
GO
-- ============================================
-- PROCEDURE: InsertOrderDetails
-- ============================================
CREATE PROCEDURE InsertOrderDetails
@OrderID INT,
@ProductID INT,
@UnitPrice MONEY = NULL,
@Quantity INT,
@Discount FLOAT = 0
AS
BEGIN
SET NOCOUNT ON;
DECLARE @Stock INT, @ReorderLevel INT, @ActualUnitPrice MONEY;
SELECT @Stock = p.Quantity, @ActualUnitPrice = pr.ListPrice
FROM Production.ProductInventory p
JOIN Production.Product pr ON pr.ProductID = p.ProductID
WHERE p.ProductID = @ProductID AND p.LocationID = 1;
SET @UnitPrice = ISNULL(@UnitPrice, @ActualUnitPrice);
IF @Quantity > @Stock
BEGIN
PRINT 'Insufficient stock. Order aborted.';
RETURN;
END
INSERT INTO Sales.SalesOrderDetail (SalesOrderID, ProductID, OrderQty, UnitPrice, SpecialOfferID, rowguid, ModifiedDate)
VALUES (@OrderID, @ProductID, @Quantity, @UnitPrice, 1, NEWID(), GETDATE());
IF @@ROWCOUNT = 0
BEGIN
PRINT 'Failed to place the order. Please try again.';
RETURN;
END
UPDATE Production.ProductInventory
SET Quantity = Quantity - @Quantity
WHERE ProductID = @ProductID AND LocationID = 1;
IF EXISTS (
SELECT 1 FROM Production.ProductInventory
WHERE ProductID = @ProductID AND Quantity < 5
)
BEGIN
PRINT 'Warning: Stock below reorder level!';
END
END;
GO
-- ============================================
-- PROCEDURE: UpdateOrderDetails
-- ============================================
CREATE PROCEDURE UpdateOrderDetails
@OrderID INT,
@ProductID INT,
@UnitPrice MONEY = NULL,
@Quantity INT = NULL,
@Discount FLOAT = NULL
AS
BEGIN
SET NOCOUNT ON;
DECLARE @OldQuantity INT, @NewQuantity INT;
SELECT @OldQuantity = OrderQty
FROM Sales.SalesOrderDetail
WHERE SalesOrderID = @OrderID AND ProductID = @ProductID;
IF @OldQuantity IS NULL
BEGIN
PRINT 'OrderID and ProductID combination not found.';
RETURN;
END
SET @NewQuantity = ISNULL(@Quantity, @OldQuantity);
SET @UnitPrice = ISNULL(@UnitPrice, (SELECT UnitPrice FROM Sales.SalesOrderDetail WHERE SalesOrderID = @OrderID AND ProductID = @ProductID));
SET @Discount = ISNULL(@Discount, 0);
UPDATE Sales.SalesOrderDetail
SET OrderQty = @NewQuantity, UnitPrice = @UnitPrice
WHERE SalesOrderID = @OrderID AND ProductID = @ProductID;
UPDATE Production.ProductInventory
SET Quantity = Quantity - (@NewQuantity - @OldQuantity)
WHERE ProductID = @ProductID AND LocationID = 1;
END;
GO
-- ============================================
-- PROCEDURE: GetOrderDetails
-- ============================================
CREATE PROCEDURE GetOrderDetails
@OrderID INT
AS
BEGIN
SET NOCOUNT ON;
IF NOT EXISTS (SELECT 1 FROM Sales.SalesOrderDetail WHERE SalesOrderID = @OrderID)
BEGIN
PRINT 'The OrderID ' + CAST(@OrderID AS VARCHAR) + ' does not exist.';
RETURN 1;
END
SELECT * FROM Sales.SalesOrderDetail WHERE SalesOrderID = @OrderID;
END;
GO
-- ============================================
-- PROCEDURE: DeleteOrderDetails
-- ============================================
CREATE PROCEDURE DeleteOrderDetails
@OrderID INT,
@ProductID INT
AS
BEGIN
SET NOCOUNT ON;
IF NOT EXISTS (
SELECT 1 FROM Sales.SalesOrderDetail
WHERE SalesOrderID = @OrderID AND ProductID = @ProductID
)
BEGIN
PRINT 'Error: Invalid OrderID or ProductID.';
RETURN -1;
END
DELETE FROM Sales.SalesOrderDetail
WHERE SalesOrderID = @OrderID AND ProductID = @ProductID;
PRINT 'Order detail successfully deleted.';
END;
GO
-- ============================================
-- VIEW: vwCustomerOrders (AdventureWorks Schema)
-- ============================================
CREATE VIEW vwCustomerOrders AS
SELECT
p.FirstName + ' ' + p.LastName AS CustomerName,
soh.SalesOrderID AS OrderID,
soh.OrderDate,
sod.ProductID,
pr.Name AS ProductName,
sod.OrderQty AS Quantity,
sod.UnitPrice,
sod.OrderQty * sod.UnitPrice AS TotalPrice
FROM Sales.SalesOrderHeader soh
JOIN Sales.SalesOrderDetail sod ON soh.SalesOrderID = sod.SalesOrderID
JOIN Production.Product pr ON sod.ProductID = pr.ProductID
JOIN Sales.Customer c ON soh.CustomerID = c.CustomerID
JOIN Person.Person p ON c.PersonID = p.BusinessEntityID;
GO
-- ============================================
-- VIEW: vwCustomerOrders_Yesterday
-- ============================================
CREATE VIEW vwCustomerOrders_Yesterday AS
SELECT
p.FirstName + ' ' + p.LastName AS CustomerName,
soh.SalesOrderID AS OrderID,
soh.OrderDate,
sod.ProductID,
pr.Name AS ProductName,
sod.OrderQty AS Quantity,
sod.UnitPrice,
sod.OrderQty * sod.UnitPrice AS TotalPrice
FROM Sales.SalesOrderHeader soh
JOIN Sales.SalesOrderDetail sod ON soh.SalesOrderID = sod.SalesOrderID
JOIN Production.Product pr ON sod.ProductID = pr.ProductID
JOIN Sales.Customer c ON soh.CustomerID = c.CustomerID
JOIN Person.Person p ON c.PersonID = p.BusinessEntityID
WHERE CAST(soh.OrderDate AS DATE) = CAST(DATEADD(DAY, -1, GETDATE()) AS DATE);
GO
-- ============================================
-- VIEW: MyProducts
-- ============================================
CREATE VIEW MyProducts AS
SELECT
p.ProductID,
p.Name AS ProductName,
p.Size AS QuantityPerUnit,
p.ListPrice AS UnitPrice,
v.Name AS CompanyName,
pc.Name AS CategoryName
FROM Production.Product p
JOIN Purchasing.ProductVendor pv ON p.ProductID = pv.ProductID
JOIN Purchasing.Vendor v ON pv.BusinessEntityID = v.BusinessEntityID
JOIN Production.ProductSubcategory psc ON p.ProductSubcategoryID = psc.ProductSubcategoryID
JOIN Production.ProductCategory pc ON psc.ProductCategoryID = pc.ProductCategoryID
WHERE p.DiscontinuedDate IS NULL;
GO
-- ============================================
-- TRIGGER: Instead Of Delete on SalesOrderHeader
-- ============================================
CREATE TRIGGER trg_InsteadOfDeleteSalesOrder
ON Sales.SalesOrderHeader
INSTEAD OF DELETE
AS
BEGIN
SET NOCOUNT ON;
DELETE FROM Sales.SalesOrderDetail
WHERE SalesOrderID IN (SELECT SalesOrderID FROM DELETED);
DELETE FROM Sales.SalesOrderHeader
WHERE SalesOrderID IN (SELECT SalesOrderID FROM DELETED);
PRINT 'Order and its details successfully deleted.';
END;
GO
-- ============================================
-- TRIGGER: Check Stock Before Insert on SalesOrderDetail
-- ============================================
CREATE TRIGGER trg_CheckStockBeforeInsert
ON Sales.SalesOrderDetail
INSTEAD OF INSERT
AS
BEGIN
SET NOCOUNT ON;
DECLARE @ProductID INT, @Quantity INT, @LocationID INT = 1, @AvailableQty INT;
DECLARE @SalesOrderID INT, @UnitPrice MONEY;
SELECT
@ProductID = ProductID,
@Quantity = OrderQty,
@SalesOrderID = SalesOrderID,
@UnitPrice = UnitPrice
FROM INSERTED;
SELECT @AvailableQty = Quantity
FROM Production.ProductInventory
WHERE ProductID = @ProductID AND LocationID = @LocationID;
IF @AvailableQty IS NULL OR @AvailableQty < @Quantity
BEGIN
PRINT 'Order could not be placed: Insufficient stock.';
RETURN;
END
INSERT INTO Sales.SalesOrderDetail (SalesOrderID, ProductID, OrderQty, UnitPrice, SpecialOfferID, rowguid, ModifiedDate)
SELECT SalesOrderID, ProductID, OrderQty, UnitPrice, 1, NEWID(), GETDATE()
FROM INSERTED;
UPDATE Production.ProductInventory
SET Quantity = Quantity - @Quantity
WHERE ProductID = @ProductID AND LocationID = @LocationID;
PRINT 'Order detail inserted and stock updated.';
END;
GO