I am trying to understand about SQL functions in Oracle, I was wondering if it is possible to write a SQL function which can return a Number or a Varchar2 based on the conditions in the program.
You can SORT OF do that using a return type of ANYDATA. But the calling query or PL/SQL block would have to use a CASE statement to evaluate the return type in order to do anything with it. Very simple demonstration below.
CREATE OR REPLACE FUNCTION AnyData_Test
(
p_Value_In IN VARCHAR2
)
RETURN AnyData
AS
ad_Test_Var ANYDATA;
BEGIN
CASE
WHEN p_Value_In = 'Varchar2' THEN ad_Test_Var := SYS.ANYDATA.ConvertVarchar2('VC2');
WHEN p_Value_In = 'Number' THEN ad_Test_Var := SYS.ANYDATA.ConvertNumber(123);
ELSE ad_Test_Var := SYS.ANYDATA.ConvertDate(SYSDATE);
END CASE;
RETURN ad_Test_Var;
END AnyData_Test;
/
SELECT
AnyData.GetTypeName(AnyData_Test('Varchar2')) AS Return_Type_V
,AnyData.AccessVarchar2(AnyData_Test('Varchar2')) AS Return_Type_V_Value
--
,AnyData.GetTypeName(AnyData_Test('Number')) AS Return_Type_N
,AnyData.GetTypeName(AnyData_Test('Nope')) AS Return_Type_D
FROM dual
;
SELECT
CASE AnyData.GetTypeName(AnyData_Test('Varchar2'))
WHEN 'SYS.VARCHAR2' THEN AnyData.AccessVarchar2(AnyData_Test('Varchar2'))
ELSE 'Other'
END AS Quick_Test
FROM dual;
Oracle functions can be as complex as you want, and you can apply as much logic as you want. A very basic example of a function with logic inside
Let's make it work
SQL> create or replace function my_example ( c1 in number , c2 in varchar2 ) return
number
is
begin
if c2 = 'A'
then
return c1 + 10 ;
elsif c2 = 'B'
then
2 3 4 5 6 7 8 9 return c1 + 100 ;
else
raise_application_error ( -20001, 'c1 only allows A or B' );
10 11 12 end if;
end;
13 14 /
Function created.
SQL> select my_example( 1 , 'A' ) from dual ;
MY_EXAMPLE(1,'A')
-----------------
11
SQL> select my_example( 1 , 'B' ) from dual ;
MY_EXAMPLE(1,'B')
-----------------
101
SQL>
UPDATE
If you want to return either numbers of strings, use return varchar2
SQL> create or replace function my_example ( c1 in number , c2 in varchar2 ) return varchar2
2 is
3 begin
if c2 = 'A'
then
return c1 + 10 ;
4 5 6 7 elsif c2 = 'B'
8 then
9 return c1 + 100 ;
10 elsif c2= 'C'
then
return 'Alfa' ;
11 12 13 else
raise_application_error ( -20001, 'c1 only allows A or B' );
end if;
end;
/ 14 15 16 17
Function created.
SQL> select my_example( 1 , 'C' ) from dual ;
MY_EXAMPLE(1,'C')
--------------------------------------------------------------------------------
Alfa
SQL> select my_example( 1 , 'A' ) from dual ;
MY_EXAMPLE(1,'A')
--------------------------------------------------------------------------------
11
SQL> select my_example( 1 , 'B' ) from dual ;
MY_EXAMPLE(1,'B')
--------------------------------------------------------------------------------
101
Then you can also apply ( 12.2 onwards ) the VALIDATE_CONVERSION
standard function to determine whether the return of the original function is a string or a number
SQL> select validate_conversion ( my_example( 1 , 'B' ) as number ) from dual ;
VALIDATE_CONVERSION(MY_EXAMPLE(1,'B')ASNUMBER)
----------------------------------------------
1
SQL> select validate_conversion ( my_example( 1 , 'C' ) as number ) from dual ;
VALIDATE_CONVERSION(MY_EXAMPLE(1,'C')ASNUMBER)
----------------------------------------------
0
SQL>
1 means true number, 0 means it is not a number
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.