【问题标题】:Executing PL/SQL procedure from python with sys.odcinumberlist as parameter使用 sys.odcinumberlist 作为参数从 python 执行 PL/SQL 过程
【发布时间】:2019-09-16 02:38:20
【问题描述】:

给定一个 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]) 但它会导致错误“参数数量或类型错误”。

【问题讨论】:

    标签: python python-3.x oracle python-2.7 cx-oracle


    【解决方案1】:

    使用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
    

    【讨论】:

    • cx_Oracle 5#但低于5.3可以吗?
    • @querysoso :我不确定。我已经使用版本7.0.0 进行了测试。
    猜你喜欢
    • 1970-01-01
    • 2017-02-18
    • 2013-04-01
    • 2021-09-04
    • 2015-01-19
    • 1970-01-01
    • 2016-08-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多