![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
I'm struggling to work out how to return an array(table) from a pl/sql function/procedure. I have tried various methods using both functions and procedures. I currently have the following code: * Package * CREATE OR REPLACE PACKAGE MyPackage as TYPE ARRAY IS TABLE OF VARCHAR2(250); PROCEDURE MyProcedure(MyArray IN OUT ARRAY); END; / CREATE OR REPLACE PACKAGE BODY MyPackage IS PROCEDURE MyProcedure(MyArray IN OUT ARRAY) is BEGIN snipped END; END; / * Procedure Call * DECLARE TYPE ARRAY IS TABLE OF VARCHAR2(250); MyArray ARRAY := LOGIN_AREA('','','','','','','','','','','','','', '',''); BEGIN MySchema.MyPackage.MyProcedure(MyArray); END; *** Do excuse the poor quality of the above code. I haven't been doing this very long. All suggestion and critism greatly recieved. Cheers Simon |
#3
| |||
| |||
|
|
i follow the syntax, but what is the error your getting? or what is being returned? are you printing out the content of the PL/SQL table to see if the values are return? im assuming your coming from an Object oriented backgroudn because MyArray ARRAY := ARRAY('','','','','','','','','','','','','','','' ); as a constructor. its not necessary. Arrays are wrapped in Oracle. Initialization is taken care of for you. |
|
please provide more detail. |
#4
| |||
| |||
|
|
"Ryan" <rgaffuri (AT) cox (DOT) net> wrote im assuming your coming from an Object oriented backgroudn because MyArray ARRAY := ARRAY('','','','','','','','','','','','','','','' ); as a constructor. its not necessary. Arrays are wrapped in Oracle. Initialization is taken care of for you. OK. I have a function which returns an array working. However the code doesn't work when I use dynamically sized arrays (using array.extend . This is because the array being returned is adifferent size to the array which I am assigning the result of the function to. How can I get around this problem? Cheers, Simon. |
#5
| |||
| |||
|
|
varray is a static array pl/sql table is a dynamic sparse array. However, you can still get null pointer exceptions with it.. which is annoying. are yo using a varray? switch to a pl/sql table. initializations is virtually the same. it never needs to be extends. Its all wrapped. I rarely see varray used and only when you want to store the array in a table which you cant do with pl/sql tables. |
|
this should fix your problem. if not provide a code snipped and the error message. |
|
im right your a c/c++ programmer right? |
#6
| |||
| |||
|
|
Confused yet? OK, as of 8i there are THREE types of collection in PL/SQL: nested tables, VARRAYs and INDEX-BY tables (formerly PL/SQL tables). What you have declared (with TYPE ARRAY IS TABLE OF VARCHAR2 (250)) is a nested table and you are correct that this must be initialised with (at least) a null table for most uses. However unless you wish to access the array directly in SQL (i.e. SELECT from it) or exchange the array with an external language (e.g. Java) then it is probably easier to use an INDEX-BY table, which is distinguished by adding the INDEX BY BINARY_INTEGER (or alternatively VARCHAR2 (n) in 9.2 and above) to the declaration. Hence your package would be declared something like... CREATE OR REPLACE PACKAGE mypackage AS TYPE array IS TABLE OF VARCHAR2(250) INDEX BY BINARY_INTEGER; PROCEDURE myprocedure ( myarray IN OUT array); END; / CREATE OR REPLACE PACKAGE BODY mypackage IS PROCEDURE myprocedure ( myarray IN OUT array) IS BEGIN myarray (1) := 'Some Value'; END; END; / One last thing - when you call this procedure you must use the TYPE declared in the package and not declare your own - the two types are not considered the same. Hence your calling block might be... DECLARE myarray mypackage.array; BEGIN mypackage.myprocedure (myarray); DBMS_OUTPUT.PUT_LINE (myarray (1)); END; / Does this help? |
![]() |
| Thread Tools | |
| Display Modes | |
| |