使用 sys.odcinumberlist 作为参数从 python 执行 PL/SQL 过程

Executing PL/SQL procedure from python with sys.odcinumberlist as parameter

给定一个 PL/SQL 过程:

PROCEDURE MyProc(myvar IN sys.odcinumberlist, curout OUT sys_refcursor);

如何使用 cx_Oracle 从 python 执行它?我在尝试

cursor.callproc('MyProc', (param, cursor_out))

参数为 [1, 2, 3]cursor.arrayvar(cx_Oracle.NUMBER, [1, 2, 3]) 但它会导致错误 'wrong number or type of arguments'.

使用conn.gettype定义SYS.ODCINUMBERLIST对象。然后用它来分配值列表(数字)

示例程序

create or replace procedure MyProc( myvar  IN  sys.odcinumberlist, 
                                    curout OUT sys_refcursor )
AS
BEGIN
    open curout for select * from TABLE(myvar);
END;
/

Python代码

conn = cx_Oracle.connect('usr/pwd@//localhost:1521/DB')
cur = conn.cursor()

tableTypeObj  = conn.gettype("SYS.ODCINUMBERLIST")
params = tableTypeObj.newobject()

po_cursor_out = cur.var(cx_Oracle.CURSOR)

params = tableTypeObj([1,2,3])

cur.callproc('hr.myproc', [ params, po_cursor_out])
result_cur = po_cursor_out.getvalue()

for row in result_cur:
    print(row[0])

结果

1
2
3