6

I am trying to call a stored procedure between python and an oracle db. The problem I am having is passing a cursor out-parameter.

The Oracle stored procedure is essentially:

create or replace procedure sp_procedure(
    cid int, 
    rep_date date,
    ret out sys_refcursor
) is
begin

  open ret for 
  select  
 ...
  end;

The python code calling to the database is:

import cx_Oracle
from datetime import date

connstr='user/[email protected]:2521/XE'
conn = cx_Oracle.connect(connstr)
curs = conn.cursor()

cid = 1
rep_date = date(2011,06,30)

curs.callproc('sp_procedure', (cid, rep_date, curs))

The error is:

curs.callproc('sp_procedure', (cid, rep_date, curs))
cx_Oracle.DatabaseError: ORA-01036: illegal variable name/number

I've also tried passing a dictionary as the keywordsParameters:

cid = 1
rep_date = date(2011,06,30)

call_params = {'cid': cid, 'rep_date': rep_date, 'ret': curs}
curs.callproc('sp_procedure', (cid, rep_date, curs), call_params)

Returns the same error.

Thanks.

1
  • Check Oracle's example ref_cursor.py for how to do this, and how to tune the fetch. Commented May 3 at 2:40

2 Answers 2

13

After a couple of hours of googling and trail/error, here's the solution:

cid = 1
rep_date = date(2011,06,30)

l_cur = curs.var(cx_Oracle.CURSOR)
l_query = curs.callproc('sp_procedure', (cid,rep_date,l_cur))

l_results = l_query[2]

for row in l_results:
    print row

# Column Specs
for row in l_results.description:
    print row
Sign up to request clarification or add additional context in comments.

1 Comment

remember all cap CURSOR
1

Try this:

curs = con.cursor() 
out_curs = con.cursor() 

curs.execute("""
BEGIN 
    sp_procedure(:cid, :rep_date, :cur); 
END;""", cid=cid, rep_date=rep_date, ret=outcurs) 

1 Comment

Hi Scott, I managed to get the callproc() method to work. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.