Skip to content

Commit f9d2b08

Browse files
committed
fix(sql): isolate explicit table hints from inner validation blocks
1 parent a44cb69 commit f9d2b08

1 file changed

Lines changed: 18 additions & 19 deletions

File tree

backend/sql/procedures/04_order_management.sql

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,16 @@
11
-- =============================================================================
22
-- SchemaForge Studio — Khulisa Commerce
33
-- Order Management Stored Procedures
4-
-- Demonstrates: SAVE TRANSACTION, nested transactions, OUTPUT clause,
5-
-- MERGE, optimistic concurrency, TRY/CATCH, XACT_ABORT
64
-- =============================================================================
75

86
USE KhulisaCommerce;
97
GO
108

11-
-- =============================================================================
12-
-- SP 1: Place a new order
13-
-- Validates stock, deducts inventory, inserts Order + OrderLines,
14-
-- applies discount, all within a single ACID transaction
15-
-- =============================================================================
169
CREATE OR ALTER PROCEDURE dbo.usp_PlaceOrder
1710
@CustomerID INT,
1811
@ShippingAddressID INT,
1912
@DiscountCode VARCHAR(50) = NULL,
2013
@OrderLinesJson NVARCHAR(MAX),
21-
-- JSON: [{"VariantID":1,"Quantity":2}, ...]
2214
@OrderID INT OUTPUT,
2315
@TotalAmount DECIMAL(10,2) OUTPUT
2416
AS
@@ -85,6 +77,9 @@ BEGIN
8577
END;
8678
END;
8779

80+
-- Explicitly terminate setup phase before transaction engine locks
81+
PRINT 'Validations passed. Beginning stock allocation.';
82+
8883
BEGIN TRANSACTION PlaceOrder;
8984
BEGIN TRY
9085

@@ -96,21 +91,25 @@ BEGIN
9691
UnitPrice DECIMAL(10,2)
9792
);
9893

94+
-- Isolated query statement to satisfy the parser
9995
INSERT INTO @StockCheck
10096
(VariantID, AvailableQty, RequestedQty, UnitPrice)
10197
SELECT
10298
sl.VariantID,
10399
sl.QuantityOnHand,
104100
l.Quantity,
105-
p.BasePrice + pv.PriceAdjustment AS UnitPrice
101+
(p.BasePrice + pv.PriceAdjustment) AS UnitPrice
106102
FROM @Lines l
107-
JOIN dbo.StockLevel sl ON sl.VariantID = l.VariantID
108-
JOIN dbo.ProductVariant pv ON pv.VariantID = l.VariantID
109-
JOIN dbo.Product p ON p.ProductID = pv.ProductID
110-
WITH (UPDLOCK, ROWLOCK);
103+
INNER JOIN dbo.StockLevel sl WITH (UPDLOCK, ROWLOCK)
104+
ON sl.VariantID = l.VariantID
105+
INNER JOIN dbo.ProductVariant pv
106+
ON pv.VariantID = l.VariantID
107+
INNER JOIN dbo.Product p
108+
ON p.ProductID = pv.ProductID;
111109

112110
-- ── Validate sufficient stock ─────────────────────────────────────────
113-
DECLARE @InsufficientVariantID INT;
111+
DECLARE @InsufficientVariantID INT = NULL;
112+
114113
SELECT TOP 1
115114
@InsufficientVariantID = VariantID
116115
FROM @StockCheck
@@ -124,7 +123,7 @@ BEGIN
124123
END;
125124

126125
-- ── Calculate totals ─────────────────────────────────────────────────
127-
DECLARE @Subtotal DECIMAL(10,2);
126+
DECLARE @Subtotal DECIMAL(10,2) = 0;
128127
DECLARE @DiscountAmount DECIMAL(10,2) = 0;
129128

130129
SELECT @Subtotal = SUM(UnitPrice * RequestedQty)
@@ -182,7 +181,7 @@ BEGIN
182181
@OrderID,
183182
'ORDER'
184183
FROM @StockCheck sc
185-
JOIN dbo.StockLevel sl ON sl.VariantID = sc.VariantID;
184+
INNER JOIN dbo.StockLevel sl ON sl.VariantID = sc.VariantID;
186185

187186
-- ── Increment discount usage counter ──────────────────────────────────
188187
IF @DiscountID IS NOT NULL
@@ -340,7 +339,7 @@ BEGIN
340339
SET sl.QuantityOnHand = sl.QuantityOnHand + ol.Quantity,
341340
sl.UpdatedAt = SYSUTCDATETIME()
342341
FROM dbo.StockLevel sl
343-
JOIN dbo.OrderLine ol ON ol.VariantID = sl.VariantID
342+
INNER JOIN dbo.OrderLine ol ON ol.VariantID = sl.VariantID
344343
WHERE ol.OrderID = @OrderID;
345344

346345
INSERT INTO dbo.StockMovement
@@ -357,7 +356,7 @@ BEGIN
357356
'RETURN',
358357
@Reason
359358
FROM dbo.OrderLine ol
360-
JOIN dbo.StockLevel sl ON sl.VariantID = ol.VariantID
359+
INNER JOIN dbo.StockLevel sl ON sl.VariantID = ol.VariantID
361360
WHERE ol.OrderID = @OrderID;
362361

363362
DECLARE @PointsToReverse INT = FLOOR(@TotalAmount / 10);
@@ -386,5 +385,5 @@ BEGIN
386385
END;
387386
GO
388387

389-
PRINT 'Order management procedures created.';
388+
PRINT 'Order management procedures created successfully.';
390389
GO

0 commit comments

Comments
 (0)