I have an Oracle stored procedure with an array as input parameter and an array as output parameter. While the input parameter already works fine, I always get back an array of null-values (although the length of the array is what I expected).
It is only a test environment so it is a trivial example: the stored procedure only takes the input array and copy the values to the output array and to a varchar2 field, so I can see that the copy from the input array to the varchar2 field works fine but not to the output-array.
My Java Code is the following:
DriverManager.registerDriver(new OracleDriver());
Connection conn = DriverManager.getConnection(
"<ConnectionString>", "<user>", "<password>");
conn.setAutoCommit(false);
OracleConnection oracleConnection = (OracleConnection)conn;
OracleCallableStatement stmt = (OracleCallableStatement)oracleConnection.prepareCall("call MYPACKAGE.TABLE_IN_TABLE_OUT( ?, ?, ? )");
String[] inputStringArray = { "1", "2", "3", "4" };
Array inputArray = oracleConnection.createOracleArray("MYPACKAGE.CHAR_TABLE", inputStringArray);
stmt.setArray(1, inputArray);
stmt.registerOutParameter(2, Types.ARRAY, "MYPACKAGE.ERG_TABLE");
stmt.registerOutParameter(3, Types.VARCHAR);
stmt.executeUpdate();
Array resultArray = stmt.getArray(2);
String [] resultStringArray = (String[])resultArray.getArray();
String resultString = stmt.getString(3);
System.out.println(resultString);
for (String result : resultStringArray) {
System.out.println(result);
}
conn.commit();
conn.close();
The stored procedure
create or replace PACKAGE MYPACKAGE IS
TYPE CHAR_TABLE IS TABLE OF CHAR(01) INDEX BY BINARY_INTEGER;
TYPE ERG_TABLE IS TABLE OF CHAR(01) INDEX BY BINARY_INTEGER;
PROCEDURE TABLE_IN_TABLE_OUT(
inputArray IN CHAR_TABLE,
outputArray OUT ERG_TABLE,
resultString OUT VARCHAR2
);
END MYPACKAGE;
The implementation of the stored procedure:
create or replace PACKAGE BODY MYPACKAGE AS
PROCEDURE TABLE_IN_TABLE_OUT(
inputArray IN CHAR_TABLE,
outputArray OUT ERG_TABLE,
resultString OUT VARCHAR2) AS
BEGIN
FOR i IN 0..inputArray.last loop
outputArray(i) := inputArray(i);
end loop;
resultString := '';
FOR i IN 0..outputArray.last loop
resultString := resultString || outputArray(i);
end loop;
END TABLE_IN_TABLE_OUT;
END MYPACKAGE;
And this is the output:
VARCHAR2 result: 1234
Array result: null, null, null, null,
After searching a lot on the internet and in this forum I really did not find out what I am doing wrong.