oracle function input with more than one possible type

I am building a function to compare 2 input variables:

CREATE FUNCTION compare_num (first IN NUMBER, second in NUMBER) 
    RETURN VARCHAR2 AS 
BEGIN
    -- write compare request into audit log database  table
    if first = second then return 0; else return 1; end if; 
END TESTFUNCTION;

then I will need to build another for date:

CREATE FUNCTION compare_date (first IN date, second in date) 
    RETURN VARCHAR2 AS 
BEGIN
    -- write compare request into audit log database  table
    if first = second then return 0; else return 1; end if; 
END TESTFUNCTION;

as you can see the 2 functions are identical except for the input type difference.

What I would like to do is build one function so I only need to write the function body once, something like:

CREATE FUNCTION compare (first IN <date OR number OR varchar2>, second in <date OR number OR varchar2>) 
    RETURN VARCHAR2 AS 
BEGIN
    -- write compare request into audit log database table
    if first = second then return 0; else return 1; end if; 
END TESTFUNCTION;

so that if I need to update “write to log” logic I don’t have to update in two places.

Is this possible?

I tried use ‘anytype’ as input types but compilation failed at “first = second” check.

New contributor

Michael Chen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Overloading

The proper way to handle this is with “overloading”. But Oracle supports overloaded procs/functions only in packages, so you need to wrap it in a package:

CREATE OR REPLACE PACKAGE pkg_test
AS
  function compare_data (first in varchar2, second in number) return varchar2;
  function compare_data (first in varchar2, second in date) return varchar2;
END;

CREATE OR REPLACE PACKAGE BODY pkg_test
AS
  function compare_data (first in varchar2, second in number) 
    return varchar2
  as
    var_second_date date;
  begin
    -- convert second to a date then call the other overload.
    var_second_date := second +...... [whatever math you need];

    RETURN compare_data(first,var_second_date);
  end;

  function compare_data (first in varchar2, second in date) 
    return varchar2
  as
  begin
    -- now do stuff and return something 
  end;
END pkg_test;

When you call pkg_test.compare_date(...,...) Oracle will detect the datatype of the literal (or bind) you are passing in for both parameters and will find the function definition in the package header that matches those datatypes… so if your second parameter is a number, it will call the first specification. If your second parameter is a date, it automatically knows to call the second one instead.

Then all you need to do to consolidate code is do whatever is number-specific and then convert it to the primary datatype (date) and call the other overload to do the main work.. again, Oracle automatically knows to call the second function rather than recursively call itself because of the datatypes being supplied in the parameters.

Generic varchar2 parameter (risky)

You could, of course, have only one function and not need packages at all if you simply use generic varchar2 parameters, but that’s generally considered bad programming because it does not enforce the real datatype validity of the parameter values. A user could pass in total junk and you’re going to end up with datatype conversion errors being thrown. You should not rely on implicit datatype conversions, and you certainly don’t want to be in the business of trying to do your own datatype inspection on a string and do conditional conversions based on what you “think” it is.

Multiple (optional) strongly-typed parameters

A third and final solution that is better than the generic varchar2 method and not as elegant as the overload is to have additional optional parameters and programming test for non-nullity and use whichever one has a non-null value. Then at least you can stay strongly-typed:

CREATE FUNCTION compare_num (first IN NUMBER, 
                             second_number in NUMBER := NULL, 
                             second_date IN date := NULL) 
    RETURN VARCHAR2 
AS 
  var_second_date date;
BEGIN
  IF second_number IS NOT NULL
  THEN
    var_second_date := second_number+..... [conversion here];
  ELSE
    var_second_date := second_date;
  END IF;

  
    -- do stuff using var_second_date
END compare_num;

The downside is that there is no enforcement that only one of the types have a value (they might both, then which do you use?), and since they now must be optional, the function could be called without any of them. So more checks and error conditions have to be built in when you do this.

So, various options, but the cleanest and most programmatically secure method is the overload approach.

Well, what you could do is to rely on Oracle’s capabilities of implicit datatype conversion and create a function which accepts parameters of a varchar2 datatype.

SQL> create or replace function compare_data
  2    (first in varchar2, second in varchar2)
  3  return number
  4  as
  5  begin
  6      if first = second then return 0; else return 1; end if;
  7  end compare_data;
  8  /

Function created.

Let’s try it:

SQL> select compare_data(1, 2) result from dual;

    RESULT
----------
         1

SQL> select compare_data('A', 'A') result from dual;

    RESULT
----------
         0

SQL> select compare_data(date '2024-10-28', date '2021-02-13') result from dual;

    RESULT
----------
         1

SQL>

Seems to be working; though, I prefer not to rely on implicit datatype conversion. If you want to take control over it, I guess you’d have to create separate functions. They could even be overloaded, i.e. look and have the same name – the only difference would be parameters’ datatype.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

oracle function input with more than one possible type

I am building a function to compare 2 input variables:

CREATE FUNCTION compare_num (first IN NUMBER, second in NUMBER) 
    RETURN VARCHAR2 AS 
BEGIN
    -- write compare request into audit log database  table
    if first = second then return 0; else return 1; end if; 
END TESTFUNCTION;

then I will need to build another for date:

CREATE FUNCTION compare_date (first IN date, second in date) 
    RETURN VARCHAR2 AS 
BEGIN
    -- write compare request into audit log database  table
    if first = second then return 0; else return 1; end if; 
END TESTFUNCTION;

as you can see the 2 functions are identical except for the input type difference.

What I would like to do is build one function so I only need to write the function body once, something like:

CREATE FUNCTION compare (first IN <date OR number OR varchar2>, second in <date OR number OR varchar2>) 
    RETURN VARCHAR2 AS 
BEGIN
    -- write compare request into audit log database table
    if first = second then return 0; else return 1; end if; 
END TESTFUNCTION;

so that if I need to update “write to log” logic I don’t have to update in two places.

Is this possible?

I tried use ‘anytype’ as input types but compilation failed at “first = second” check.

New contributor

Michael Chen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Overloading

The proper way to handle this is with “overloading”. But Oracle supports overloaded procs/functions only in packages, so you need to wrap it in a package:

CREATE OR REPLACE PACKAGE pkg_test
AS
  function compare_data (first in varchar2, second in number) return varchar2;
  function compare_data (first in varchar2, second in date) return varchar2;
END;

CREATE OR REPLACE PACKAGE BODY pkg_test
AS
  function compare_data (first in varchar2, second in number) 
    return varchar2
  as
    var_second_date date;
  begin
    -- convert second to a date then call the other overload.
    var_second_date := second +...... [whatever math you need];

    RETURN compare_data(first,var_second_date);
  end;

  function compare_data (first in varchar2, second in date) 
    return varchar2
  as
  begin
    -- now do stuff and return something 
  end;
END pkg_test;

When you call pkg_test.compare_date(...,...) Oracle will detect the datatype of the literal (or bind) you are passing in for both parameters and will find the function definition in the package header that matches those datatypes… so if your second parameter is a number, it will call the first specification. If your second parameter is a date, it automatically knows to call the second one instead.

Then all you need to do to consolidate code is do whatever is number-specific and then convert it to the primary datatype (date) and call the other overload to do the main work.. again, Oracle automatically knows to call the second function rather than recursively call itself because of the datatypes being supplied in the parameters.

Generic varchar2 parameter (risky)

You could, of course, have only one function and not need packages at all if you simply use generic varchar2 parameters, but that’s generally considered bad programming because it does not enforce the real datatype validity of the parameter values. A user could pass in total junk and you’re going to end up with datatype conversion errors being thrown. You should not rely on implicit datatype conversions, and you certainly don’t want to be in the business of trying to do your own datatype inspection on a string and do conditional conversions based on what you “think” it is.

Multiple (optional) strongly-typed parameters

A third and final solution that is better than the generic varchar2 method and not as elegant as the overload is to have additional optional parameters and programming test for non-nullity and use whichever one has a non-null value. Then at least you can stay strongly-typed:

CREATE FUNCTION compare_num (first IN NUMBER, 
                             second_number in NUMBER := NULL, 
                             second_date IN date := NULL) 
    RETURN VARCHAR2 
AS 
  var_second_date date;
BEGIN
  IF second_number IS NOT NULL
  THEN
    var_second_date := second_number+..... [conversion here];
  ELSE
    var_second_date := second_date;
  END IF;

  
    -- do stuff using var_second_date
END compare_num;

The downside is that there is no enforcement that only one of the types have a value (they might both, then which do you use?), and since they now must be optional, the function could be called without any of them. So more checks and error conditions have to be built in when you do this.

So, various options, but the cleanest and most programmatically secure method is the overload approach.

Well, what you could do is to rely on Oracle’s capabilities of implicit datatype conversion and create a function which accepts parameters of a varchar2 datatype.

SQL> create or replace function compare_data
  2    (first in varchar2, second in varchar2)
  3  return number
  4  as
  5  begin
  6      if first = second then return 0; else return 1; end if;
  7  end compare_data;
  8  /

Function created.

Let’s try it:

SQL> select compare_data(1, 2) result from dual;

    RESULT
----------
         1

SQL> select compare_data('A', 'A') result from dual;

    RESULT
----------
         0

SQL> select compare_data(date '2024-10-28', date '2021-02-13') result from dual;

    RESULT
----------
         1

SQL>

Seems to be working; though, I prefer not to rely on implicit datatype conversion. If you want to take control over it, I guess you’d have to create separate functions. They could even be overloaded, i.e. look and have the same name – the only difference would be parameters’ datatype.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật