【问题标题】:Passing list to PL/SQL procedure将列表传递给 PL/SQL 过程
【发布时间】:2019-12-03 10:38:10
【问题描述】:

前段时间有一个类似的question。我尝试遵循建议的结果:

Traceback (most recent call last):
  File "<input>", line 1, in <module>
cx_Oracle.DatabaseError: ORA-06550: line 1, column 7:
PLS-00306: wrong number or types of arguments in call to 'TEST_ARRAY'
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored

这是我到目前为止所做的。

1.创建新类型

create type list_ids as table of int;

2。创建测试程序

create or replace procedure test_array(ids in list_ids) is
begin
    for i in 1..ids.count
    loop
        dbms_output.PUT_LINE(ids(i));
    end loop;
end;

3.运行 python 脚本

import cx_Oracle


db = cx_Oracle.connect("my_user", "my_password", "<host>:<port>/<sid>")
cursor = db.cursor()

idl = cursor.arrayvar(cx_Oracle.NUMBER, [1,2,3,4])
cursor.callproc("test_array", parameters=[idl])

Traceback (most recent call last):
  File "<input>", line 1, in <module>
cx_Oracle.DatabaseError: ORA-06550: line 1, column 7:
PLS-00306: wrong number or types of arguments in call to 'TEST_ARRAY'
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored

那么我错过了什么?

【问题讨论】:

  • the linked question 中,答案都使用 PL/SQL 关联数组 (TYPE name AS TABLE OF INT INDEX BY PLS_INTEGER) 并且不使用 SQL 集合作为传递的数据类型。这几乎肯定是该问题的重复。 documentation for cursor.arrayvar 没有明确说明它是仅适用于 PL/SQL 关联数组还是也适用于 SQL 集合,所以我将保持打开状态,以防有人可以发布后者的示例。
  • 感谢您指出 PL/SQL 关联数组和 SQL 集合之间的差异方向。这是我忽略的一点(对我来说是新的)。

标签: python oracle plsql cx-oracle user-defined-types


【解决方案1】:

如上面的 cmets 中所述,cursor.arrayvar() 仅可用于 PL/SQL 关联数组,不适用于嵌套表。对于嵌套表,您需要使用“对象”API。这个example 展示了如何做到这一点,即使它恰好也用于 PL/SQL 关联数组!

我还调整了cursor.arrayvar() 的文档,以明确它仅适用于具有连续键的 PL/SQL 关联数组。

【讨论】:

  • 非常感谢。使用对象 API 比在过程中将关联数组转换为嵌套表要优雅得多。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-07-05
  • 1970-01-01
  • 2021-10-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-22
相关资源
最近更新 更多