tags:

views:

444

answers:

1

I have a function that takes as one of it's arguments a VARRAY of pl/sql Objects. How do I execute this stored procedure and bind the resultset that it returns to a data grid in TOAD for Oracle?

+2  A: 

After some searching around, I found the answer to my own problem. Say your varray type was called varchar_pair_array and the objects stored in this array were called varchar_pair_object. varchar_pair_object is a simple object that has two varchars as it's members.

Here is the code for executing a proc that takes in a varray of varchar_pair_object (s):

DECLARE 
  RetVal SYS_REFCURSOR;
  a_simplevalue VARCHAR2(200);
  another_simplevalue VARCHAR2(200);
  my_array_of_varchar_pairs VARCHAR_PAIR_ARRAY; -- assume varchar_pair_array is defined somewhere else
  my_obj VARCHAR_PAIR_OBJECT; -- assume varchar_pair_object is defined somewhere else
  my_other_obj VARCHAR_PAIR_OBJECT;
BEGIN 
  a_simplevalue := 'hello';
  another_simplevalue := 'there';
  my_obj := VARCHAR_PAIR_OBJECT('nice to meet you', 'greetings');
  my_other_obj := VARCHAR_PAIR_OBJECT('goodbye', 'ciao');
  my_array_of_varchar_pairs := VARCHAR_PAIR_ARRAY(); 
  my_array_of_varchar_pairs.EXTEND(2); -- this should be the number of objects you plan to put into the array
  my_array_of_varchar_pairs(1) := my_obj;
  my_array_of_varchar_pairs(2) := my_other_obj; 

  RetVal := my_function ( a_simplevalue, another_simplevalue, my_array_of_varchar_pairs); -- assuming your array takes two varchars and one array of VARCHAR_PAIR_OBJECT (s)
  :to_grid := RetVal;

END;

Copy paste this code in TOAD's sql editor and change it to adapt to your function and types and hit F9. TOAD will ask you the type of the :to_grid variable. Select cursor (assuming your function returns a ref cursor) and hit enter. TOAD will bind the result set to a data grid.

Links that helped me:

http://www.smart-soft.co.uk/Oracle/oracle-plsql-tutorial-part-11.htm (good tutorial on collections) http://download.oracle.com/docs/cd/B10501_01/appdev.920/a96624/10_objs.htm#1972 (especially useful in this case is the section on declaring and initializing objects)

With very little change the same can be done with a procedure.

neesh
Also have a look at the "Execute Procedure" function in the Schema Browser. It has an option to load ref cursor results into a grid after execution. It also has the advantage that it will remember the parameters you used last time you executed that procedure.
Charles
The problem there is that with complex types such as varray and objects it won't (can't?) generate the sql for complex types and you are on your own initializing them.
neesh
True, but that's why it lets you edit the code before executing it :)
Charles
yes. In fact, most of this code is what was generated by TOAD from execute procedure. I just didn't know how to edit that code to pass in an array of objects which is why I asked the question here originally. Thanks.
neesh