CREATE or replace function get_data(data_key integer) return sys_refcursor
is
result1 sys_refcursor;
BEGIN
open result1 for 'Select DISTINCT COL1
FROM REF_TABLE
where DATA_KEY='||data_key;
return result1;
END;
variable rc refcursor;
exec :rc :=get_data(30038);
print rc;
这可以从sql developer正常工作。 如何使用python和sqlalchemy调用它?
答案 0 :(得分:0)
我使用cx_Oracle而不是SQLAlchemy而且它有效。结果输出与我对sqlalchemy的预期相同。我将该功能转换为程序。
CREATE or replace procedure get_data(data_key in integer, result out
sys_refcursor) as
BEGIN
open result for 'Select DISTINCT COL1
FROM ref_table
where DATA_KEY='||data_key;
END;
import cx_Oracle
import pandas as pd
conn = cx_Oracle.connect('system/<password>@127.0.0.1/XE')
cur = conn.cursor()
myvar = cur.var(cx_Oracle.CURSOR)
cur.callproc('get_data', (30038, myvar))
data = myvar.getvalue().fetchall()
if len(data) == 0:
data = {}
df = pd.DataFrame(data, columns=[i[0] for i in myvar.getvalue().description])
print df
输出上述代码
COL1
--------------------------
0 219586
1 246751
2 228245
3 244517
4 220765
5 243467
6 246622
7 222784