Answer: Parameterized Stored Procedures
Explore how to define and call parameterized stored procedures that allow dynamic input values to be passed at runtime. Understand the use of DELIMITER, procedure creation, and execution to efficiently filter and retrieve data. Learn alternate approaches such as prepared statements and views, and deepen your SQL skills for intermediate interview scenarios.
Solution
The solution is given below:
Code explanation
The explanation of the solution code is given below:
Line 3: The
DELIMITER $$changes the statement delimiter to$$so semicolons can be used within the procedure.Lines 5–6: The
CREATE PROCEDUREdefines a stored procedure calledGetSalesByCategoryAndMonthwith two parameters:catIDandsalesMonth.Lines 7–11: The
BEGINandENDare used to define the body of the stored procedure. TheSELECTstatement retrieves the data from theSalestable. TheWHEREclause filters the record(s) with respect to the specified condition.Line 13: The
DELIMITER ;resets the statement delimiter back to the ...