3

I am trying to call a stored procedure from python using sqlalchemy. The code I have is as follows:

@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)

I get the following error: AttributeError: 'pyodbc.Connection' object has no attribute 'callproc'

I followed the instructions on the official documentation but that did not make a difference.

I am on python 3.5

Is there a different way to call stored procedures?

1 Answer 1

5

From https://github.com/mkleehammer/pyodbc/wiki/Calling-Stored-Procedures, pyodbc does not implement .callproc() method on the connection object. You must execute the sp using a SQL call (.execute("{CALL GetAdvMedianTrailing30Days}")).

(This isn't really a SQLAlchemy question since you are leaking the abstraction down to make a DB-API call using pyodbc)

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks! I get an error that pyodbc.ProgrammingError: ('42000', "[42000] [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]The EXECUTE permission was denied on the object 'GetAdvMedianTrailing30Days', database 'XXX', schema 'dbo'. (229) (SQLExecDirectW)"). How can I resolve this?
Does the user you are using sqlalchemy to log into the server with have the necessary permissions to execute GetAdvMedianTrailing30Days in that database?
Will have to ask my team :P . The github link you posted is really helpful, since my stored proc returns values I will have to convert it into a SQL statement I think. Thanks again!
The error you are getting implies the problem is permissions on the db server, not related to python.
yeah, i thought so too. I will have to look into SQL permissions for the user I am using.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.