How to retrieve result of Oracle database function via ODBC?

怎甘沉沦 提交于 2019-12-20 05:41:59

问题


I have problem with calling Oracle stored function (not procedure) via ODBC.

My function is really simple, it just concatenates two strings. I can call it via:

rs = c.execute("SELECT add_str('yogi', 'bubu') FROM dual")
for row in c.fetchall():
    print(row[0])

But such type of calling database function will not work for functions that changes database. So I tried this:

c.execute("{ ? = call add_str('ala', 'bubu') }");

But I got:

Error: HY000: The driver did not supply an error!

In ODBC trace file it looks like:

python.exe odbc a20-e68 ENTER SQLExecDirect 
        HSTMT               00A02CE0
        UCHAR *             0x00AA6CE4 [      -3] "{ ? = call add_str('ala', 'bubu') }\ 0"
        SDWORD                    -3

python.exe odbc a20-e68 EXIT  SQLExecDirect  with return code -1 (SQL_ERROR)
        HSTMT               00A02CE0
        UCHAR *             0x00AA6CE4 [      -3] "{ ? = call add_str('ala', 'bubu') }\ 0"
        SDWORD                    -3

python.exe odbc a20-e68 ENTER SQLGetDiagRecW 
        SQLSMALLINT                  3 
        SQLHANDLE           00A02CE0
        SQLSMALLINT                  1 
        SQLWCHAR *          0x0021F7BC (NYI) 
        SQLINTEGER *        0x0021F808
        SQLWCHAR *          0x00A035F8 (NYI) 
        SQLSMALLINT               1023 
        SQLSMALLINT *       0x0021F818

python.exe odbc a20-e68 EXIT  SQLGetDiagRecW  with return code 100 (SQL_NO_DATA_FOUND)
        SQLSMALLINT                  3 
        SQLHANDLE           00A02CE0
        SQLSMALLINT                  1 
        SQLWCHAR *          0x0021F7BC (NYI) 
        SQLINTEGER *        0x0021F808
        SQLWCHAR *          0x00A035F8 (NYI) 
        SQLSMALLINT               1023 
        SQLSMALLINT *       0x0021F818

Similar operation is possible with JDBC/Jython:

proc = db.prepareCall("{ ? = call add_str('j_bubu', 'j_yogi') }");
proc.registerOutParameter(1, Types.VARCHAR)
r = proc.execute();
print(proc.getString(1))

so it should be possible to do similar thing using ODBC & Python. I have checked that if I used JDBC-ODBC bridge then Jython can call database function via ODBC just like native JDBC driver.

My environment:

Database: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production

ODBC driver: 11.01.00.06

SQORA32.DLL FileVersion: 11.1.0.6.0

I tried odbc module from ActiveState Python 2.7 and pyodbc module.

My question:

Is there any way of calling Oracle database function (not procedure) via their ODBC driver? And how to retrieve its result?


回答1:


Google brought up this page:

http://www.gossamer-threads.com/lists/python/python/71449

... I don't think ODBC supports stored procedures returning a value directly. Only as part of a cursor. ...

Basically, you can't call the function directly. You could, however, create a procedure with an OUT parameter instead.



来源:https://stackoverflow.com/questions/11541894/how-to-retrieve-result-of-oracle-database-function-via-odbc

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