2011-11-08 6 views
0

私は、整数の配列を返すPL/SQL関数を記述しようとしています。そして、cx_Oracles callfuncでそれを呼び出すことができます。私はPL/SQL関数を正しく持っていると思いますが、cx_Oracleを使って呼び出す方法はわかりません。cx_oracle:callfuncはリストを返しますか?

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; 

これは、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) 

どのように私はcx_Oracleを使用して、このような関数の結果を得ることができますか?それは可能ですか?

私はこれを見つけましたhttp://osdir.com/ml/python.db.cx-oracle/2005-06/msg00014.htmlしかし、私は本当にそれを使用する方法を知りません。私は私のタイプの定義を変更する場合:

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

私が取得: 警告:コンパイルエラーで作成タイプ。

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 

答えて

1

manualのこの部分を読ん

import cx_Oracle 

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

conn_data = str('%s/%[email protected]%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() 

、のラインで何も有用であろう。

+0

この行のreturn_valueは次のとおりです。 'execute_func = cursor.callfunc( 'test_function'、return_value、[20])'は定義されていません。それは何でしょうか? – Maciek

+0

Mea culpa、 私はout_parameterに編集しました。 –

+0

残念ながら、これは動作しません。 ORA-06550: cx_Oracle.DatabaseErrorで ファイル ""、行1、:私は >>> execute_func = cursor.callfunc( 'test_function'、out_parameter、[20]) トレースバック(最新の呼び出しの最後)を取得します:行1、列13: PLS-00382:式のタイプが間違っています ORA-06550:行1、列7: PL/SQL:ステートメントが無視されます – Maciek

関連する問題