7

I've created a Python script to execute an SP using my cursor with connection odbc. I've tried everything I found but I can not execute the desired procedure. My code below:

import pyodbc
conn = pyodbc.connect( 'DRIVER={SQL Server};'
                           'SERVER=XXXXXX;'
                           'DATABASE=XXX;UID=XXXX;'
                           'PWD=XXX')
cursor = conn.cursor()
cmd_prod_executesp = 'EXECUTE DC_SAS_EvaluationUpdate'
cursor.execute(cmd_prod_executesp)
conn.close()

I have tried:

cmd_prod_executesp = '{call DC_SAS_EvaluationUpdate}'
cmd_prod_executesp = 'exec DC_SAS_EvaluationUpdate'
cmd_prod_executesp = '{CALL DC_SAS_EvaluationUpdate}'

I appreciate your help

thanks so much

16
  • does your sp take any parameters? Commented Jul 20, 2017 at 19:24
  • @OLIVER.KOO not buddy is just a simple execute but it is driving me crazy!! Commented Jul 20, 2017 at 19:28
  • try putting the sql directly in the execute() like this cursor.execute(" exec DC_SAS_EvaluationUpdate") also do you get any errro messages? Commented Jul 20, 2017 at 19:34
  • 1
    I was wondering maybe you don't have the correct driver. But another reason could be you didn't commit after you execute your sp. so put conn.commit() after cursor.execute() Commented Jul 20, 2017 at 20:11
  • 1
    Buddy figured it out doing conn.autocommit = True. Even if I did a commit within my SP thanks buddy. How can reach you on LinkedIn or something like that. thanks @GordThompson Commented Jul 20, 2017 at 21:26

3 Answers 3

9

Thanks so much everyone for your comments. Finally, in a quick comment from @GordThompson I applied the changes below and that worked.

import pyodbc


conn = pyodbc.connect( 'DRIVER={SQL Server};'
                       'SERVER=XXXX;'
                       'DATABASE=XX;UID=XXXX;'
                       'PWD=XXXX')
cursor = conn.cursor()

cmd_prod_executesp = """EXEC DC_SAS_EvaluationUpdate """
conn.autocommit = True
cursor.execute(cmd_prod_executesp)

conn.close()
Sign up to request clarification or add additional context in comments.

1 Comment

you could also set auto commit to true when you create the connection. It is seen more common that way. conn = pyodbc.connect( 'DRIVER={SQL Server};' 'SERVER=XXXX;' 'DATABASE=XX;UID=XXXX;' 'PWD=XXXX;' 'autocommit=True;') remember In pyodbc.connect() autocommit is disabled by default
1

Here are two examples that work for me. I'll show with parameters, because I'm guessing you'll need to know how to do that as well.

With named parameters:

cursor.execute('EXEC usp_get_user_data @name = ?, @username = ?', 'tim', 'flipperpa')

With positional parameters:

cursor.execute('EXEC usp_get_user_data ?, ?', None, 'flipperpa')

Good luck!

6 Comments

I have tried even CALL and it doesnt work :( OMG. The most weird thing guys is that I have already performed Selects and inserts successfully in the same server, it's just the execution for SP that it doesnt work
IIRC, CALL is for MySQL. Can you try creating a really simple stored procedure that just returns something simple like SELECT 'HELLO WORLD' and try executing that?
buddy when I go to DB and execute manually that works thats so so weird. I will create my scripts in a GitHub repository to show you guys more details about my script
yep you want to use exec? since exec is for SQL server format cursor.execute("exec sp_dosomething(123, 'abc')") and call is for ODBC format cursor.execute("{call sp_dosomething(123, 'abc')}")
Are you connecting through SSMS as the same user you're connecting from Python as?
|
1

I think your code looking fine as you did follow the guideline from pyodbc

//SQL Server format

cursor.execute("exec sp_dosomething(123, 'abc')")

//ODBC format

cursor.execute("{call sp_dosomething(123, 'abc')}")

From your comment you said you see the message

`Process finished with exit code 0'

That means that everything worked ok.If any exception/error happened in your program your program should generate an exit code with non-zero argument.

So I think your program worked fine and the SP did get executed. However, I suspect you don't see the changes take effect in your DB could be because you didn't commit() your changes after you execute your sp and before you close your connection to your DB.

so add conn.commit() like this:

cursor.execute(cmd_prod_executesp)
conn.commit()

EDIT: try to use the newer driver for MsSQL as well

 conn = pyodbc.connect(driver = '{SQL Server Native Client 10.0}', server = 'xxxxxx',database = 'xxxx', uid = 'xx', pwd = 'xxxx')

Comments

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.