Skip to Main Content

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, Punit.

Asked: February 03, 2002 - 1:44 pm UTC

Last updated: February 10, 2020 - 4:19 am UTC

Version: 8.1.7

Viewed 1000+ times

You Asked

Can U Please explain me how can I create a procedure or function which can accept a variable number of parameter values. For example, How can i create a simple function ADD() which may accept any number of values and return me the sum. Oracle has in-built functions such as Greatest, Least, etc. which are capable of accepting any number of parameters - Is that possible for a developer to create such a function ?

Thanks for ur help

and Tom said...

Like this:

ops$tkyte@ORA817DEV.US.ORACLE.COM> create or replace type numArray as table of number
  2  /

Type created.

ops$tkyte@ORA817DEV.US.ORACLE.COM> 
ops$tkyte@ORA817DEV.US.ORACLE.COM> 
ops$tkyte@ORA817DEV.US.ORACLE.COM> create or replace function addemup( p_data in numArray ) return number
  2  as
  3          l_total number;
  4  begin
  5          for i in 1 .. p_data.count
  6          loop
  7                  l_total := nvl(l_total,0) + nvl(p_data(i),0);
  8          end loop;
  9          return l_total;
 10  end;
 11  /

Function created.

ops$tkyte@ORA817DEV.US.ORACLE.COM> 
ops$tkyte@ORA817DEV.US.ORACLE.COM> 
ops$tkyte@ORA817DEV.US.ORACLE.COM> select addemup( numArray( 1, 2, 3, 4, 5 ) )
  2    from dual;

ADDEMUP(NUMARRAY(1,2,3,4,5))
----------------------------
                          15

ops$tkyte@ORA817DEV.US.ORACLE.COM> 
ops$tkyte@ORA817DEV.US.ORACLE.COM> select addemup( numArray( 1, 2, 3 ) )
  2    from dual;

ADDEMUP(NUMARRAY(1,2,3))
------------------------
                       6

ops$tkyte@ORA817DEV.US.ORACLE.COM> 
ops$tkyte@ORA817DEV.US.ORACLE.COM> select addemup( cast( multiset( select user_id from all_users ) as numArray ) )
  2    from dual;

ADDEMUP(CAST(MULTISET(SELECTUSER_IDFROMALL_USERS)ASNUMARRAY))
-------------------------------------------------------------
                                                         2138

ops$tkyte@ORA817DEV.US.ORACLE.COM> 
ops$tkyte@ORA817DEV.US.ORACLE.COM> select sum(user_id) from all_users;

SUM(USER_ID)
------------
        2138


 

Rating

  (4 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

NumArray

JG, February 04, 2002 - 3:45 am UTC

Excellet example as always

Simply Excellent

shankar, February 05, 2002 - 4:59 am UTC

Your code very useful besides being simple.Thanks

Oleksandr Alesinskyy, February 05, 2002 - 5:28 am UTC


What about Varchar2 type?

Hong, February 07, 2020 - 8:24 pm UTC

Help!! I have user pass in variable number of parameters(like department numbers) to function that I need to use in the where clause like "DeptID in (Deptlist)". I have:

create or replace type t_Dept as table of VARCHAR2(10);

create or replace function TestDept (Depts t_Dept) return Varchar2
as
DeptList Varchar2(250) :='';
begin
for n in 1..Depts.count
loop
if n=1 then
DeptList := Depts(n);
else
DeptList := DeptList || ''',''' ||Depts(n);
end if;

end loop;
if Depts.count>1 then
DeptList:='''' || DeptList || '''';
end if;
return DeptList;

End;
/

one parameter works, not more than 1.:
select * from tblDept where DeptID in (TestDept(t_Dept('590100'))); works

select * from tblDept where DeptID in (TestDept(t_Dept('590100', '590101'))); --Not working.
Maybe there are quotation being passed for character type?
Connor McDonald
February 10, 2020 - 4:19 am UTC

select * from tblDept where DeptID in (
select column_value from table(t_Dept('590100', '590101')));

More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library