Solved

OVERLOADING a FUNCTION

Posted on 2007-11-26
6
538 Views
Last Modified: 2013-12-07
I am OVERLOADING a package.

I have a PACKAGE called VERIFY_CARD_TYPE .  It in are two functions, into which I pass a credit_card_number.

The FUNCTIONS are called IS_IT_FSA.  One returns a BOOLEAN value and the other returns a VARCHAR


What I need to know is; can I choose to test either the BOOELAN or VARCHAR returned values?  Or do I have to specify which of the FUNCTIONS I want to execute?
0
Comment
Question by:joekeri
  • 3
  • 3
6 Comments
 
LVL 18

Expert Comment

by:Jinesh Kamdar
Comment Utility
I am not sure if functions returning diff. data-types, but accepting similar data-types can be overloaded successfully. For a function to be over-loaded, Oracle must be able to identify which signature to call at run-time for which it relies on the function definition indicated by the parameters accepted by the function, not by the return type. However, if more than 1 function with same signature, then Oracle cannot determine which one to call (see attached for demo).

In your case, it would be better to use a simple procedure with 2 OUT parameters - 1 for BOOLEAN and other for VARCHAR2 and use any 1 of these parameters depending on the logic.
SQL> create or replace package jak_pkg as

  2  

  3  function jak_fun return boolean;

  4  function jak_fun return varchar2;

  5  

  6  end jak_pkg;

  7  /
 

Package created.
 

SQL> create or replace package body jak_pkg as

  2  

  3  function jak_fun return boolean is

  4  begin

  5  return true;

  6  end;

  7  

  8  function jak_fun return varchar2 is

  9  begin

 10  return 'true';

 11  end;

 12  

 13  end jak_pkg;

 14  /
 

Package body created.
 

SQL> declare

  2  v boolean;

  3  begin

  4  v := jak_pkg.jak_fun;

  5  dbms_output.Put_line(v);

  6  end;

  7  /

v := jak_pkg.jak_fun;

             *

ERROR at line 4:

ORA-06550: line 4, column 14:

PLS-00307: too many declarations of 'JAK_FUN' match this call

ORA-06550: line 4, column 1:

PL/SQL: Statement ignored

ORA-06550: line 5, column 1:

PLS-00306: wrong number or types of arguments in call to 'PUT_LINE'

ORA-06550: line 5, column 1:

PL/SQL: Statement ignored
 

SQL> declare

  2  v varchar2(10);

  3  begin

  4  v := jak_pkg.jak_fun;

  5  dbms_output.Put_line(v);

  6  end;

  7  /

v := jak_pkg.jak_fun;

             *

ERROR at line 4:

ORA-06550: line 4, column 14:

PLS-00307: too many declarations of 'JAK_FUN' match this call

ORA-06550: line 4, column 1:

PL/SQL: Statement ignored
 

SQL> 

Open in new window

0
 

Author Comment

by:joekeri
Comment Utility
Here is my SPEC and BODY. From what I can see, my example is similar in structure to yours.

CREATE OR REPLACE PACKAGE PKG_is_it_fsa_card AS
FUNCTION IS_IT_FSA_CARD (P_card_number_decrypted IN VARCHAR2)
         RETURN BOOLEAN deterministic;

FUNCTION IS_IT_FSA_CARD (P_card_number_decrypted IN VARCHAR2)
         RETURN VARCHAR2 deterministic;
END PKG_is_it_fsa_card;
~~~~~~~~~~~~~~~~~~~~~~
CREATE OR REPLACE PACKAGE BODY PKG_is_it_fsa_card IS

-------------------------------------------------------------------------------
FUNCTION is_it_fsa_card (P_card_number_decrypted IN varchar2)
RETURN BOOLEAN IS

   l_starting_number       fsa_bin_ext.starting_number%type := null;
   

BEGIN
  SELECT FSA.starting_number
    INTO l_starting_number
    FROM FSA_BIN_EXT FSA
    WHERE P_card_number_decrypted BETWEEN FSA.starting_number and FSA.ending_number;
   

  RETURN (TRUE);
 
 
EXCEPTION
   WHEN NO_DATA_FOUND THEN
        RETURN (FALSE);
   WHEN TOO_MANY_ROWS THEN
        RETURN (TRUE);
   WHEN OTHERS THEN
        RETURN (FALSE);
END is_it_fsa_card;
--------------------------------------------------------------------------------
FUNCTION is_it_fsa_card (P_card_number_decrypted IN varchar2)
RETURN varchar2 IS

   l_starting_number             fsa_bin_ext.starting_number%type := null;
   l_is_it_fsa_flag              varchar2(1)    := 'Y';
     

BEGIN
  SELECT FSA.starting_number
    INTO l_starting_number
    FROM FSA_BIN_EXT FSA
    WHERE P_card_number_decrypted BETWEEN FSA.starting_number and FSA.ending_number;
   

  RETURN (l_is_it_fsa_flag );
 
 
EXCEPTION
   WHEN NO_DATA_FOUND THEN
        l_is_it_fsa_flag   := 'N';
        RETURN (l_is_it_fsa_flag );
   WHEN TOO_MANY_ROWS THEN
        l_is_it_fsa_flag   := 'Y';
        RETURN (l_is_it_fsa_flag );
   WHEN OTHERS THEN
        l_is_it_fsa_flag   := 'N';
        RETURN (l_is_it_fsa_flag );
END is_it_fsa_card;
END PKG_is_it_fsa_card;
/
0
 
LVL 18

Expert Comment

by:Jinesh Kamdar
Comment Utility
Yes, your example is pretty much similar as mine. Unfortunately (as evident in my post too) Oracle does not throw any error when you compile this pkg. body as it does not compare the function signatures at compile-time. However, if you try to call this function from outside, you should be thrown an error. Just fire this and see what happens : -

SELECT PKG_is_it_fsa_card.is_it_fsa_card('TEST')
FROM DUAL;
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:joekeri
Comment Utility
I ran SELECT PKG_is_it_fsa_card.is_it_fsa_card('TEST')
FROM DUAL;

and this is the error i got back

ORA-06553: PLS-307: too many declarations of 'IS_IT_FSA_CARD' match this call
0
 
LVL 18

Accepted Solution

by:
Jinesh Kamdar earned 50 total points
Comment Utility
Correct. As i said, Oracle could not determine the right signature to call, so it threw an error.
Essentially, functions should be over-loaded only when you have to perform similar operations on a variety of data-types e.g. addition operation for a string would be diff. from the same for a date. However, in your case, since both of your functions process the same type of data i.e. VARCHAR2 (credit_card_no), you should use only a single function returning either a BOOLEAN or a VARCHAR2, based on your requirements.
0
 

Author Closing Comment

by:joekeri
Comment Utility
I took your sugesstion and abandoned the OVERLOADing function and had it just return a BOOLEAN.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

How to Create User-Defined Aggregates in Oracle Before we begin creating these things, what are user-defined aggregates?  They are a feature introduced in Oracle 9i that allows a developer to create his or her own functions like "SUM", "AVG", and…
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now