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
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.
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.
Internally, the Varargs method is implemented by using the single dimensions arrays concept.
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 .
you have at least 3 options:
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!;' );
/* ... */
create or replace type strings_type as table of varchar2(50);
select * from table (strings_type ('DD','CC','EE'));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With