cx_oracle: ¿puede la lista de devolución de callfunc?

I'm trying to write a PL/SQL function that returns an array of integers and then be able to call it with cx_Oracles callfunc. I think I got the PL/SQL function right, but I don't know how to call it with cx_Oracle.

create or replace type test_type is table of NUMBER(10);

create or replace function test_function (n in INTEGER)
RETURN test_type
AS
  tmp_tab test_type := test_type();
BEGIN
  tmp_tab.EXTEND(n);
  FOR i IN 1 .. n LOOP
    tmp_tab(i) := i;
  END LOOP;
  RETURN tmp_tab;
END;

It works with sqlplus:

SQL> select test_function(20) from dual;

TEST_FUNCTION(20)
--------------------------------------------------------------------------------
TEST_TYPE(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20)

How can I get the result of such function using cx_Oracle? Is that possible?

He encontrado esto http://osdir.com/ml/python.db.cx-oracle/2005-06/msg00014.html but I don't realy know how to use it. When I change my type definition to:

create or replace type test_type is table of NUMBER(10) index by binary_integer;

I get: Warning: Type created with compilation errors.

SQL> sho err
Errors for TYPE TEST_TYPE:

LINE/COL ERROR
-------- -----------------------------------------------------------------
0/0  PL/SQL: Compilation unit analysis terminated
1/19     PLS-00355: use of pl/sql table not allowed in this context

preguntado el 08 de noviembre de 11 a las 09:11

1 Respuestas

Something in the lines of,

import cx_Oracle

db_sid = 'db_sid'
db_usr = 'schema'
db_pwd = 'passwd'

conn_data = str('%s/%s@%s') % (db_usr, db_pwd, db_sid)

try:
    db = ora.connect(conn_data)
    except ora.DatabaseError, e:
    error, = e
    ORAmessage = error.message.rstrip("\n")
    print "DatabaseError: %s" % ORAmessage
else:
    cursor = db.cursor()
    try:
        out_parameter = cursor.var(cx_Oracle.NUMBER)
        # calling function to retrieve results until 20
        execute_func  = cursor.callfunc('test_function', out_parameter, [20])
        print str(return_value.getvalue())
    except ora.DatabaseError, exc:
        error, = exc
        ORAmessage = error.message.rstrip("\n")
        print "DatabaseError: %s" % ORAmessage
    cursor.close()

db.close()

Reading this part of the manual también será útil.

respondido 08 nov., 11:18

The return_value in this line: execute_func = cursor.callfunc('test_function', return_value, [20]) is not defined. What should it be? - barriga

Unfortunately this doesn't work. I get >>> execute_func = cursor.callfunc('test_function', out_parameter, [20]) Traceback (most recent call last): File "<stdin>", line 1, in <module> cx_Oracle.DatabaseError: ORA-06550: line 1, column 13: PLS-00382: expression is of wrong type ORA-06550: line 1, column 7: PL/SQL: Statement ignored - barriga

It's because you're returning as *TEST_TYPE*(1, 2, 3, 4,... ) and not accessing columns in a database that are numbers. Try switching cx_Oracle.NUMBER to cx_Oracle.OBJECT. - Joao Figueiredo

>>> out_parameter = cursor.var(cx_Oracle.OBJECT) Traceback (most recent call last): TypeError: expecting type name for object variables - barriga

I'm sorry to not be of further help, my PL/SQL experience is quite limited, I usually access both procedures and functions with .cursor.callfunc with no issues. - Joao Figueiredo

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.