cx_Oracle And User Defined Types

佐手、 提交于 2019-12-06 13:37:31

While cx_Oracle can select user defined types, it does not to my knowledge support passing in user defined types as bind variables. So for example the following will work:

cursor.execute("select my_type('foo', 'bar', 'hello') from dual")
val, = cursor.fetchone()
print val.COMPONENT, val.KEY, val.VALUE

However what you can't do is construct a Python object, pass it in as an input argument and then have cx_Oracle "translate" the Python object into your Oracle type. So I would say you're going to have to construct your input argument within a PL/SQL block.

You can pass in Python lists, so the following should work:

components=["foo", "faz"]
values=["bar", "baz"]
keys=["hello", "world"]
cursor.execute("""
declare
  type udt_StringList is table of varchar2(4000) index by binary_integer;
  l_components udt_StringList := :p_components;
  l_keys udt_StringList := :p_keys;
  l_values udt_StringList := :p_values;
  l_parms my_type_tab;
begin
  l_parms.extend(l_components.count);
  for i in 1..l_components.count loop
    l_parms(i) := my_type(l_components(i), l_keys(i), l_values(i));
  end loop;

  my_package.my_procedure(l_parms);
end;""", p_components=components, p_values=values, p_keys=keys)

Are you trying to populate the table of objects more efficiently?

If you can do a SELECT, have a look at the BULK COLLECT INTO clause

I'm not quite sure what you mean by hard-coded, but you can build a dynamic array like this:

SQL> desc my_procedure
Parameter Type        Mode Default? 
--------- ----------- ---- -------- 
P_IN      MY_TYPE_TAB IN   

SQL> declare
  2     l_tab my_type_tab;
  3  begin
  4     select my_type(owner, table_name, column_name)
  5       bulk collect into l_tab
  6       from all_tab_columns
  7      where rownum <= 10;
  8     my_procedure (l_tab);
  9  end;
 10  /

PL/SQL procedure successfully completed

This has been tested with Oracle 11.1.0.6.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!