简体   繁体   中英

PLSQL Function with variable number of parameters

I have to write a generic plsql function which is supposed to submit any concurrent request. For example, If the concurrent program has 5 parameters my function also should take 5 parameters as input. Like wise if the concurrent program has n number of parameters my function should take n number of input parameters. Can this be achieved? Please suggest the best way.

I tried manually with If statements like IF no of program parameters = 5, API should submit for the 5 input arguments only. But I know this is not the best way as we don't know which program end user may submit and how many parameters it has.

You can create a function that has multiple parameters and set them to "default null". Then just check if they are null or not.

Something like

function test(p1 in varchar2 default null, p2 in varchar2 default null, p3 in varchar2 default null, p4 in varchar2 default null, p5 in varchar2 default null, p6 in varchar2 default null, p7 in varchar2 default null, p8 in varchar2 default null, p9 in varchar2 default null, p10 in varchar2 default null, etc..) return number is
begin
  if p1 is not null then
  .....
  end if;
end test;

Or you can make the function accept an array, like.

create function test(p_args in sys.dbms_sql.varchar2_table) return number is
begin
  for i in 1..p_args.count loop
    ....
  end loop;
end test;

Or for a bit more flexibility have it accept a JSON array, that way you can pass a mixed use of scalar types.

create function test(p_jarr in json_array_t) return number is
begin
  for i in 0..p_jarr.get_size-1 loop
    if p_jarr.get_type(i) = 'SCALAR' and p_jarr.get(i).is_string then
      // do something with string
    end if;
    if p_jarr.get_type(i) = 'SCALAR' and p_jarr.get(i).is_number then
      // do something with number
    end if;
  end loop;
end test;
// check for boolean, date, timestamp, 

Regards

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM