Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

OVERLOADING a FUNCTION

Posted on 2007-11-26
6
Medium Priority
?
570 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
ID: 20351041
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
ID: 20351076
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
ID: 20351126
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
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 

Author Comment

by:joekeri
ID: 20351237
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 200 total points
ID: 20351279
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
ID: 31411000
I took your sugesstion and abandoned the OVERLOADing function and had it just return a BOOLEAN.
0

Featured Post

[Webinar] Cloud Security

In this webinar you will learn:

-Why existing firewall and DMZ architectures are not suited for securing cloud applications
-How to make your enterprise “Cloud Ready”, and fix your aging DMZ architecture
-How to transform your enterprise and become a Cloud Enabler

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
Shell script to create broker configuration file using current broker Configuration, solely for purpose of backup on Linux. Script may need to be modified depending on OS-installation. Please deploy and verify the script in a test environment.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.
Suggested Courses

963 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