🔔 Master Database Programming Today!
Subscribe to our channel for practical, beginner-friendly tutorials on SQL, MySQL, Oracle DB, and Java!
▶ Subscribe to Ram N JavaIntroduction: Why Use Multiple Input Parameters?
When querying databases, filtering by a single criterion is rarely enough. In real-world database development, stored procedures often need to accept multiple input parameters (IN) to filter, search, or process datasets based on combinations of criteria—such as country, district, minimum population, or date ranges.
In this guide, you will learn how to create and execute a MySQL stored procedure that takes multiple input parameters and returns the matching records directly.
Step 1: Defining Multiple Input Parameters
In MySQL, each input parameter is declared with the keyword IN followed by the variable name and its data type, separated by commas:
- param_country_code (IN VARCHAR): Accepts the target country code.
- param_district (IN VARCHAR): Accepts the target district or region name.
Step 2: Creating the Stored Procedure
Inside the procedure block, you can use standard SQL statements like SELECT combined with the AND operator to filter using both parameters:
CREATE PROCEDURE `get_cities_by_country_and_district` (
IN param_country_code VARCHAR(3),
IN param_district VARCHAR(20)
)
BEGIN
SELECT ID, Name, CountryCode, District, Population
FROM City
WHERE CountryCode = param_country_code
AND District = param_district;
END
In MySQL Workbench, click Apply to review the SQL definition and compile the stored routine.
Step 3: Calling the Stored Procedure
To run a stored procedure with multiple input arguments, pass them in the exact order defined:
-- Execute with multiple argument values
CALL get_cities_by_country_and_district('AFG', 'Kabol');
MySQL evaluates both filtering conditions simultaneously and returns a clean result grid matching both parameters.
No comments:
Post a Comment