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

问题描述 投票:3回答:1

给定PL / SQL过程:

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

如何使用cx_Oracle从python执行它?我在努力

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

param是[1, 2, 3]cursor.arrayvar(cx_Oracle.NUMBER, [1, 2, 3]),但它导致错误'错误的数字或参数类型'。

python python-3.x oracle python-2.7 cx-oracle
1个回答
2
投票

使用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
© www.soinside.com 2019 - 2024. All rights reserved.