Procedure overloading in PL/SQL

Procedure overloading in PL/SQL refers to the ability to define multiple procedures (or functions) with the same name but with different parameter lists. Each version of the procedure is differentiated by the number of parameters, their types, or the o…


This content originally appeared on DEV Community and was authored by Pranav Bakare

Procedure overloading in PL/SQL refers to the ability to define multiple procedures (or functions) with the same name but with different parameter lists. Each version of the procedure is differentiated by the number of parameters, their types, or the order of their types.

This allows you to perform the same operation with different kinds of input without having to create uniquely named procedures for each case.

Rules for Procedure Overloading:

  1. Different number of parameters: Procedures can have the same name if they have a different number of parameters.

  2. Different parameter types: Procedures can also be overloaded if the types of their parameters differ.

  3. Order of parameter types: The same name can be used if the order of parameters with different types is changed.

Example:

Here’s an example of procedure overloading in PL/SQL:

-- Procedure 1: Takes one parameter (employee_id)
CREATE OR REPLACE PROCEDURE get_employee_info (p_employee_id IN NUMBER) AS
BEGIN
DBMS_OUTPUT.PUT_LINE('Employee ID: ' || p_employee_id);
END;
/

-- Procedure 2: Takes two parameters (employee_id and employee_name)
CREATE OR REPLACE PROCEDURE get_employee_info (p_employee_id IN NUMBER, p_employee_name IN VARCHAR2) AS
BEGIN
DBMS_OUTPUT.PUT_LINE('Employee ID: ' || p_employee_id || ', Name: ' || p_employee_name);
END;
/

-- Procedure 3: Takes two parameters (employee_name and hire_date)
CREATE OR REPLACE PROCEDURE get_employee_info (p_employee_name IN VARCHAR2, p_hire_date IN DATE) AS
BEGIN
DBMS_OUTPUT.PUT_LINE('Employee Name: ' || p_employee_name || ', Hire Date: ' || p_hire_date);
END;
/

Usage:

You can call any of the overloaded procedures, and PL/SQL will automatically choose the right one based on the parameters provided.

BEGIN
-- Calls the first procedure (by passing just employee_id)
get_employee_info(1001);

-- Calls the second procedure (by passing employee_id and employee_name)
get_employee_info(1002, 'John Doe');

-- Calls the third procedure (by passing employee_name and hire_date)
get_employee_info('Jane Smith', SYSDATE);

END;
/

Output:

Employee ID: 1001
Employee ID: 1002, Name: John Doe
Employee Name: Jane Smith, Hire Date: 06-OCT-2024

Key Points:

Procedures with the same name but different parameters help improve code readability.

Overloading makes the procedure more flexible since it can handle different input types or numbers of inputs.

PL/SQL automatically determines which procedure to execute based on the arguments passed.


This content originally appeared on DEV Community and was authored by Pranav Bakare


Print Share Comment Cite Upload Translate Updates
APA

Pranav Bakare | Sciencx (2024-10-06T03:20:30+00:00) Procedure overloading in PL/SQL. Retrieved from https://www.scien.cx/2024/10/06/procedure-overloading-in-pl-sql/

MLA
" » Procedure overloading in PL/SQL." Pranav Bakare | Sciencx - Sunday October 6, 2024, https://www.scien.cx/2024/10/06/procedure-overloading-in-pl-sql/
HARVARD
Pranav Bakare | Sciencx Sunday October 6, 2024 » Procedure overloading in PL/SQL., viewed ,<https://www.scien.cx/2024/10/06/procedure-overloading-in-pl-sql/>
VANCOUVER
Pranav Bakare | Sciencx - » Procedure overloading in PL/SQL. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/10/06/procedure-overloading-in-pl-sql/
CHICAGO
" » Procedure overloading in PL/SQL." Pranav Bakare | Sciencx - Accessed . https://www.scien.cx/2024/10/06/procedure-overloading-in-pl-sql/
IEEE
" » Procedure overloading in PL/SQL." Pranav Bakare | Sciencx [Online]. Available: https://www.scien.cx/2024/10/06/procedure-overloading-in-pl-sql/. [Accessed: ]
rf:citation
» Procedure overloading in PL/SQL | Pranav Bakare | Sciencx | https://www.scien.cx/2024/10/06/procedure-overloading-in-pl-sql/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.