Skip to Main Content
  • Questions
  • PASSING A ASSOCIATIVE ARRAY INTO A STORED FUNCTION WITHIN PLSQL

Breadcrumb

Question and Answer

Tom Kyte

Thanks for the question.

Asked: June 12, 2008 - 9:53 am UTC

Last updated: June 12, 2008 - 4:04 pm UTC

Version: 9.0.1

Viewed 10K+ times! This question is

You Asked

Hi,

I hope you can help me, I have created a simple function within a package which I have shown below. This function converts all the values within an associaitve array into one varchar.

PACKAGE SPEC

TYPE assoc_array IS TABLE OF VARCHAR2 (1000);


FUNCTION f_assoc_array (
inputs IN assoc_array
)
RETURN VARCHAR2;


PACKAGE BODY assoc
IS
FUNCTION f_assoc_array (
inputs IN assoc_array
)
RETURN VARCHAR2
IS
str_of_inputs VARCHAR2 (32767);
BEGIN
FOR i IN inputs.FIRST .. inputs.LAST
LOOP
CASE i
WHEN inputs.COUNT
THEN
str_of_inputs :=
str_of_inputs || '''' || inputs (i) || ''''
|| ')';
ELSE
str_of_inputs :=
str_of_inputs || '''' || inputs (i) || ''''
|| ',';
END CASE;
END LOOP;

RETURN str_of_inputs;
END f_assoc_array;
end assoc;

Everytime I try to test this function using the following script I receive the following error message.

PLS-00306 wrong number or types of arguments in call to 'string'

DECLARE
TYPE zzz IS TABLE OF VARCHAR2 (32767)
INDEX BY PLS_INTEGER;
abc zzz;
yyy varchar2(1000);
BEGIN
abc (1) := 'Steve';
abc (2) := 'Paul';
yyy:= ass_array.f_ass_array (abc);
DBMS_OUTPUT.put_line (yyy);
END;

and Tom said...


ops$tkyte%ORA10GR2> declare
  2          abc ASSOC.ASSOC_ARRAY := ASSOC.ASSOC_ARRAY('Steve', 'Paul' );
  3  begin
  4          dbms_output.put_line( assoc.f_assoc_array(abc) );
  5  end;
  6  /
'Steve','Paul')

PL/SQL procedure successfully completed.




you need to use the same type in the call as you used in the function itself. Just because to you and me the types look "the same", they are different types - you only define the type ONCE and then use it in all of the calls.


by the way - assoc_array type, it is a collection, it is not an associative array, it is just a normal "index me with an integer" array.


create type associative_array as table of number index by varchar2(30);


Now that type, that would an associative array, there you could:


l_data('Steve') := 42;
l_data('Paul' ) := 55;



Rating

  (2 ratings)

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

Comments

Thank-you

Steve Vickery, June 13, 2008 - 5:32 am UTC

Thank-you, with your help I have resolved the issue.

Thanks

srp, May 14, 2009 - 11:33 am UTC

Thanks Tom,
This resolved big part of my issue.


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