Using Parameters (IN, OUT, INOUT) in SQL Procedures
📖 Scenario: You work at a small bookstore that keeps track of book sales and inventory in a database. You want to create a stored procedure to update the stock of a book after a sale and also return the updated stock count.
🎯 Goal: Create a stored procedure named UpdateStock that uses IN, OUT, and INOUT parameters to update the stock of a book and return the updated stock count.
📋 What You'll Learn
Create a table named
Books with columns BookID (integer), Title (string), and Stock (integer).Insert three books with specific stock values.
Create a stored procedure
UpdateStock with parameters:IN p_BookID INT - the ID of the book to update,IN p_Sold INT - the number of books sold,OUT p_NewStock INT - to return the updated stock,INOUT p_Adjustment INT - an adjustment value to add to stock after sale.Inside the procedure, update the stock by subtracting
p_Sold and adding p_Adjustment.Set
p_NewStock to the updated stock value.Update
p_Adjustment to zero after applying it.💡 Why This Matters
🌍 Real World
Bookstores and many businesses use stored procedures with parameters to update inventory and return results efficiently.
💼 Career
Understanding how to use IN, OUT, and INOUT parameters in SQL procedures is essential for database developers and administrators to write reusable and efficient database code.
Progress0 / 4 steps