Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oracle equivalent of Java's Varargs

I'm trying to create a PL/SQL Procedure which can handle an unknown, but relatively small, number of Strings/Varchar2s as a parameter. The equivalent in Java could be to use Varargs:

public void foo(String... bar) {
}

Looking at Oracle's documentation on Collections and Records it would seem that using an Associative Array is a suitable choice, but I'm not certain.

Can you please tell me if Associative Array is the correct way to go?

If it's possible to pass in an anonymous Associative Array when invoking the call to the Procedure?

Thanks

like image 460
Levity Avatar asked May 14 '13 08:05

Levity


People also ask

What type is Varargs?

Varargs is a short name for variable arguments. In Java, an argument of a method can accept arbitrary number of values. This argument that can accept variable number of values is called varargs.

Can Varargs take an array?

Every time we use varargs, the Java compiler creates an array to hold the given parameters. In this case, the compiler creates an array with generic type components to hold the arguments. The varargs usage is safe if and only if: We don't store anything in the implicitly created array.

Which data structure is used by Varargs in Java?

Internally, the Varargs method is implemented by using the single dimensions arrays concept.

Can Varargs be null Java?

As Java cannot determine the type of literal null , you must explicitly inform the type of the literal null to Java. Failing to do so will result in an NullPointerException . For example, the following main method invokes the foo method with literal null .


2 Answers

you have at least 3 options:

  1. (standard) use an associative array as a procedure parameter
  2. define a 'sufficient' number of optional formal parameters
  3. use a single varchar parameter with a defined separator char

sample code 1.)

TYPE t_map IS TABLE OF VARCHAR2(50) INDEX BY VARCHAR2(20);

CREATE OR REPLACE PROCEDURE demo_1 ( vararg IN t_map ) IS
BEGIN
   /* whatever */
   NULL;
END demo_1;

/* ... somewhere else ... */
my_var t_map;

my_var('first') := 'this';
my_var('next')  := ' is a ';
my_var('last')  := 'demo';

demo_1 ( my_var );
/* ... */

sample code 2.) (max. 5 parameters)

CREATE OR REPLACE PROCEDURE demo_2 (
      vararg1 IN  VARCHAR2 DEFAULT NULL
    , vararg2 IN  VARCHAR2 DEFAULT NULL
    , vararg3 IN  VARCHAR2 DEFAULT NULL
    , vararg4 IN  VARCHAR2 DEFAULT NULL
    , vararg5 IN  VARCHAR2 DEFAULT NULL
) IS
BEGIN
   /* whatever */
   NULL;
END demo_2;

/* ... somewhere else ... */
demo_2 ( 'this', ' is a ', 'demo' );
/* ... */

sample code 3.) ( special char being ';' - must not occur inside the payload data )

CREATE OR REPLACE PROCEDURE demo_3 (
      vararg IN  VARCHAR2
) IS
   l_arg2 VARCHAR2(50);
   l_arg5 VARCHAR2(50);
BEGIN
   l_arg2 := SUBSTR(REGEXP_SUBSTR(vararg, ';[^;]*', 1, 2), 2);
   l_arg5 := SUBSTR(REGEXP_SUBSTR(vararg, ';[^;]*', 1, 5), 2);
   /* whatever */
END demo_3;

/* ... somewhere else ... */
demo_3 ( ';this; is a ;demo;;really!;' );
/* ... */
like image 180
collapsar Avatar answered Sep 28 '22 11:09

collapsar


create or replace type strings_type as table of varchar2(50);
select * from table (strings_type ('DD','CC','EE'));
like image 24
Rodriguez Avatar answered Sep 28 '22 12:09

Rodriguez