We need a stored procedure that needs to be executed whenever a customer places an order. The
Fantastic news! We've Found the answer you've been seeking!
Question:
- We need a stored procedure that needs to be executed whenever a customer places an order. The stored procedure checks the existing stock in the inventory db and decides whether the order can be placed or not based on the existing stock and the quantity requested. If the order can be placed, the requested quantity is deducted from the stock and the new stock is updated. The structure of the stored procedure is as follows -
- Name of the stored procedure: _spPlaceOrder
- Take in productid (id of a product from inventory table) and quantity (a random integer) as parameters
- Initiate a transaction.
- Insert a record in Orders table with order_date (current date), productid (as received by the stored procedure), and quantity (as received by the stored procedure).
- Store the orderId of the newly inserted record in a variable.
- Fetch the existing quantity and price of the requested product from Inventory table in stock_count column and price column respectively.
- If existing stock count is equal or more than the quantity requested, deduct the requested quantity from the stock count, update the total column of the current order in Orders table and commit the transaction. Print the message 'Order Placed Successfully.'
- If existing stock count is less than the quantity requested, rollback the transaction and print the message 'Insufficient stock to fulfil the order.
Related Book For
Smith and Roberson Business Law
ISBN: 978-0538473637
15th Edition
Authors: Richard A. Mann, Barry S. Roberts
Posted Date: