Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pass array to oracle procedure

I want to send two arrays form java to oracle stored procedures. The first Array is array of strings and the second is array of chars how can I make this??

like image 513
palAlaa Avatar asked Mar 04 '11 20:03

palAlaa


People also ask

How do you pass an array as a parameter in PL SQL?

SQL> SQL> DECLARE 2 P_ARRAY NUM_ARRAY := NUM_ARRAY(null); 3 BEGIN 4 5 P_ARRAY(1) := 15; 6 P_ARRAY(2) := 54; 7 P_ARRAY(3) := 56; 8 9 END; 10 / DECLARE * ERROR at line 1: ORA-06533: Subscript beyond count ORA-06512: at line 6 SQL> SQL> SQL> DECLARE 2 P_ARRAY NUM_ARRAY := NUM_ARRAY(null); 3 BEGIN 4 P_ARRAY.

How do I pass a list as parameter in SQL stored procedure?

CREATE FUNCTION dbo. SplitInts ( @List VARCHAR(MAX), @Delimiter VARCHAR(255) ) RETURNS TABLE AS RETURN ( SELECT Item = CONVERT(INT, Item) FROM ( SELECT Item = x.i.value('(./text())[1]', 'varchar(max)') FROM ( SELECT [XML] = CONVERT(XML, '<i>' + REPLACE(@List, @Delimiter, '</i><i>') + '</i>'). query('.


2 Answers

Here's an example of how to do it.

The following script sets up a table, a type and a stored procedure in the database. The procedure takes a parameter of the array type and inserts each row of the array into the table:

CREATE TABLE strings (s VARCHAR(4000));

CREATE TYPE t_varchar2_array AS TABLE OF VARCHAR2(4000);
/

CREATE OR REPLACE PROCEDURE p_array_test(
    p_strings      t_varchar2_array
)
AS
BEGIN
  FOR i IN 1..p_strings.COUNT
  LOOP
    INSERT INTO strings (s) VALUES (p_strings(i));
  END LOOP;
END;
/

The Java code then demonstrates passing an array into this stored procedure:

import java.sql.*;
import oracle.jdbc.*;
import oracle.sql.*;

public class ArrayTest {
    public static void main(String[] args) throws Exception {
        DriverManager.registerDriver(new OracleDriver());
        Connection conn = DriverManager.getConnection(
            "jdbc:oracle:thin:@localhost:1521:xe", "user", "pass");

        CallableStatement stmt = conn.prepareCall("BEGIN p_array_test(?); END;");

        // The first parameter here should be the name of the array type.
        // It's been capitalised here since I created it without using
        // double quotes.
        ArrayDescriptor arrDesc =
            ArrayDescriptor.createDescriptor("T_VARCHAR2_ARRAY", conn);

        String[] data = { "one", "two", "three" };
        Array array = new ARRAY(arrDesc, conn, data);
        stmt.setArray(1, array);
        stmt.execute();

        conn.commit();
        conn.close();
    }
}

If you run the SQL script and then the Java class, and then query the table strings, you should find that all of the data has been inserted into the table.

When you say 'an array of chars', I'm guessing that you mean an array of Java chars. If I've guessed right, then I think you'd be best off converting the chars to Strings and then using the same approach as above.

like image 187
Luke Woodward Avatar answered Oct 04 '22 02:10

Luke Woodward


Look here: http://download.oracle.com/docs/cd/B19306_01/java.102/b14355/oraarr.htm#i1058512

and here is my short example:

1) on database

SQL> create or replace type string_array as table of varchar2(100);
  2  /

Type created.

SQL> create or replace function to_string(p_array in string_array) return varchar2
  2  as
  3     l_string varchar2(32767);
  4     i binary_integer;
  5  begin
  6     i := p_array.first();
  7     while i is not null loop
  8        l_string := l_string || p_array(i) || ';';
  9        i := p_array.next(i);
 10     end loop;
 11     l_string := rtrim(l_string, ';');
 12     return l_string;
 13  end;
 14  /

Function created.

2) in java

public class ArrayTest {
    public static void main(String[] args) throws SQLException {
        DriverManager.registerDriver(new OracleDriver());
        OracleConnection connection = (OracleConnection) DriverManager.getConnection(...);

        String[] elements = {"abc", "def", "geh"};
        ArrayDescriptor descriptor = ArrayDescriptor.createDescriptor("STRING_ARRAY", connection);
        ARRAY array = new ARRAY(descriptor, connection, elements);

        OracleCallableStatement stmt = (OracleCallableStatement) connection.prepareCall("{? = call to_string(?)}");
        stmt.registerOutParameter(1, Types.VARCHAR);
        stmt.setARRAY(2, array);
        stmt.execute();

        String result = stmt.getString(1);
        System.out.println("to_string returned: " + result);
    }
}

seems to work: output says

to_string returned: abc;def;geh
like image 35
Marcin Wroblewski Avatar answered Oct 04 '22 02:10

Marcin Wroblewski