2017-08-02 132 views
1

我想从python使用sqlalchemy调用stored procedure。我的代码如下:Python sqlalchemy调用存储过程

@database_connection_wrapper 
def get_adv(connection): 
    ''' get the adv using a stored proc from the database ''' 

    connection.callproc("GetAdvMedianTrailing30Days") 
    results = list(connection.fetchall()) 
    print(results) 

我得到以下错误: AttributeError: 'pyodbc.Connection' object has no attribute 'callproc'

我跟着官方文件中的说明,但没有有所作为。

我对python 3.5

是否有不同的方法来调用stored procedures

回答

2

https://github.com/mkleehammer/pyodbc/wiki/Calling-Stored-Procedures,pyodbc没有对连接对象执行.callproc()方法。您必须使用SQL调用来执行sp(.execute("{CALL GetAdvMedianTrailing30Days}"))。

(这是不是一个真正的问题,SQLAlchemy的,因为你正在泄漏的抽象下来要使用pyodbc一个DB-API调用)

+0

谢谢! pyodbc.ProgrammingError('42000',“[42000] [Microsoft] [用于SQL Server的ODBC驱动程序13] [SQL Server]的错误在对象'GetAdvMedianTrailing30Days',数据库'XXX'上拒绝EXECUTE权限, ,schema'dbo'(229)(SQLExecDirectW)“)'。我该如何解决这个问题? – PYA

+0

请问您使用SQLAlchemy的用户登录到服务器具有在数据库中执行'GetAdvMedianTrailing30Days'必要的权限? – cowbert

+0

必须问我的团队:P。你发布的github链接真的很有用,因为我存储的proc返回值,我将不得不将它转换成我认为的SQL语句。再次感谢! – PYA