-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaterialManagementSystem.sql
More file actions
146 lines (121 loc) · 4.04 KB
/
MaterialManagementSystem.sql
File metadata and controls
146 lines (121 loc) · 4.04 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
USE AdventureWorks;
GO
IF OBJECT_ID('mms.ReplenishInventory', 'P') IS NOT NULL
DROP PROCEDURE mms.ReplenishInventory;
GO
IF OBJECT_ID('mms.UpdateInventoryQuantity', 'P') IS NOT NULL
DROP PROCEDURE mms.UpdateInventoryQuantity;
GO
IF OBJECT_ID('mms.StockChangeLog', 'U') IS NOT NULL
DROP TABLE mms.StockChangeLog;
GO
IF EXISTS (SELECT * FROM sys.schemas WHERE name = 'mms')
DROP SCHEMA mms;
GO
CREATE SCHEMA mms;
GO
CREATE TABLE mms.StockChangeLog (
LogID INT IDENTITY(1,1) PRIMARY KEY,
ProductID INT NOT NULL,
LocationID SMALLINT NOT NULL,
OldQuantity SMALLINT,
NewQuantity SMALLINT,
ChangeDate DATETIME DEFAULT GETDATE(),
ChangedBy SYSNAME DEFAULT SYSTEM_USER
);
GO
CREATE OR ALTER PROCEDURE mms.ReplenishInventory
@ReplenishQty SMALLINT
AS
BEGIN
SET NOCOUNT ON;
DECLARE
@ProductID INT,
@LocationID SMALLINT,
@Quantity SMALLINT,
@SafetyStock SMALLINT,
@NewQty SMALLINT;
DECLARE inventory_cursor CURSOR FOR
SELECT pi.ProductID, pi.LocationID, pi.Quantity
FROM Production.ProductInventory pi
INNER JOIN Production.Product p ON pi.ProductID = p.ProductID;
BEGIN TRY
OPEN inventory_cursor;
FETCH NEXT FROM inventory_cursor INTO @ProductID, @LocationID, @Quantity;
WHILE @@FETCH_STATUS = 0
BEGIN
SELECT @SafetyStock = SafetyStockLevel
FROM Production.Product
WHERE ProductID = @ProductID;
IF @Quantity < @SafetyStock
BEGIN
BEGIN TRANSACTION;
SET @NewQty = @Quantity + @ReplenishQty;
UPDATE Production.ProductInventory
SET Quantity = @NewQty,
ModifiedDate = SYSDATETIME()
OUTPUT
inserted.ProductID,
inserted.LocationID,
deleted.Quantity AS OldQuantity,
inserted.Quantity AS NewQuantity,
GETDATE(),
SYSTEM_USER
INTO mms.StockChangeLog(ProductID, LocationID, OldQuantity, NewQuantity, ChangeDate, ChangedBy)
WHERE ProductID = @ProductID AND LocationID = @LocationID;
COMMIT TRANSACTION;
END
FETCH NEXT FROM inventory_cursor INTO @ProductID, @LocationID, @Quantity;
END
CLOSE inventory_cursor;
DEALLOCATE inventory_cursor;
END TRY
BEGIN CATCH
IF @@TRANCOUNT > 0
ROLLBACK TRANSACTION;
CLOSE inventory_cursor;
DEALLOCATE inventory_cursor;
THROW;
END CATCH
END
GO
CREATE OR ALTER PROCEDURE mms.UpdateInventoryQuantity
@ProductID INT,
@LocationID SMALLINT,
@NewQuantity SMALLINT
AS
BEGIN
SET NOCOUNT ON;
DECLARE @OldQuantity SMALLINT;
BEGIN TRY
BEGIN TRANSACTION;
IF EXISTS (
SELECT 1 FROM Production.ProductInventory
WHERE ProductID = @ProductID AND LocationID = @LocationID
)
BEGIN
SELECT @OldQuantity = Quantity
FROM Production.ProductInventory
WHERE ProductID = @ProductID AND LocationID = @LocationID;
UPDATE Production.ProductInventory
SET Quantity = @NewQuantity,
ModifiedDate = SYSDATETIME()
WHERE ProductID = @ProductID AND LocationID = @LocationID;
INSERT INTO mms.StockChangeLog(ProductID, LocationID, OldQuantity, NewQuantity)
VALUES(@ProductID, @LocationID, @OldQuantity, @NewQuantity);
END
ELSE
BEGIN
INSERT INTO Production.ProductInventory(ProductID, LocationID, Shelf, Bin, Quantity, Rowguid, ModifiedDate)
VALUES(@ProductID, @LocationID, 'A', 0, @NewQuantity, NEWID(), SYSDATETIME());
INSERT INTO mms.StockChangeLog(ProductID, LocationID, OldQuantity, NewQuantity)
VALUES(@ProductID, @LocationID, 0, @NewQuantity);
END
COMMIT TRANSACTION;
END TRY
BEGIN CATCH
ROLLBACK TRANSACTION;
THROW;
END CATCH
END
GO