Write a Procedure to Log Inventory Change
InventoryLog Table:
QueryExplanation:
InventoryLog Table:
Stores details like product ID, old quantity, new quantity, and timestamp of change.
Stored Procedure LogInventoryChange
:
Takes 3 inputs: @ProductID
, @OldQuantity
, and @NewQuantity
.
Inserts a new record into the InventoryLog
table each time it's executed.
Procedure Execution:
The EXEC
line simulates a change in inventory for product ID 101
, from quantity 50
to 30
.
SQL Query:
-- Step 1: Create the InventoryLog table (if not already exists)
-- This table will store the inventory change logs
CREATE TABLE InventoryLog (
LogID INT IDENTITY(1,1) PRIMARY KEY,
ProductID INT,
OldQuantity INT,
NewQuantity INT,
ChangedOn DATETIME DEFAULT GETDATE()
);
GO
-- Step 2: Create the stored procedure to log inventory change
CREATE PROCEDURE dbo.LogInventoryChange
@ProductID INT,
@OldQuantity INT,
@NewQuantity INT
AS
BEGIN
-- Insert a record into the log table capturing inventory changes
INSERT INTO InventoryLog (ProductID, OldQuantity, NewQuantity)
VALUES (@ProductID, @OldQuantity, @NewQuantity);
END;
GO
-- Step 3: Execute the procedure to log an inventory change
EXEC dbo.LogInventoryChange @ProductID = 101, @OldQuantity = 50, @NewQuantity = 30;
Output:
Trainings :
Data Science Training in Indore | Data Analytics Training in Indore | Python Training in Indore | Blockchain Training in Indore | React JS Training in Indore | Web Development Training in Indore | Full Stack Development Training in Indore |Free Courses and Resource :
Verbal Aptitude | Matplotlib Tutorials | Examples | Interview Questions | Pandas Tutorials | Projects | Interview Questions | OpenCV Tutorials | Projects | Interview Questions | ExpressJS Tutorials | Projects | Interview Questions | Dart | Python Data Structures and Algorithms | Rust Tutorials | Projects | Interview Questions | Aptitude Tests | HackerRank Python | Django | MongoDB Tutorials | Examples | Interview Questions | NumPy Practice Questions | Python Tutorials by CodersDaily | Golang Tutorials | Projects | Interview Questions | React.js Tutorials | Verbal Ability Tutorial | MS Sql Server Tutorials | Examples | Interview Questions | Power BI Tutorials | Projects | Interview Questions | Example Dashboards | Numpy Tutorials | Projects | Interview Questions | Django REST Framework Tutorial | HackerRank C++ Solutions | Tensor Flow | Pandas Practice Questions | Python Practice Questions | C++ Tutorials | Quantitative Ability Tutorial | Javascript | Node.js Tutorials | HackerRank SQL Solutions | HackerRank DSA Solutions | Verbal Aptitude 2 | HackerRank Java Solutions | HTML Tutorial | TCS NQT Mock Test Series | Reasoning Ability Tutorial | CodeChef Python Solutions | HackerRank C Program Solutions | Leetcode Python Solutions | SQL Practice Question | Matplotlib Practice Questions |Interview Questions :
Pandas Tutorials | Projects | Interview Questions | ExpressJS Tutorials | Projects | Interview Questions | Django | Python Tutorials by CodersDaily | Golang Tutorials | Projects | Interview Questions | Numpy Tutorials | Projects | Interview Questions | Django REST Framework Tutorial |Top Colleges in India :
Indian Institute of Technology Bombay | Jaypee University of Engineering and Technology - Guna |