Python Forum

Full Version: execute a SQL stored proc within PYTHON
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am trying to execute a stored proc using the following i keep getting a error?
am I using the correct syntax to call the sql stored proc




import PYODBC





db = pyodbc.connect("Driver={SQL Server Native Client 11.0};"

"Server=server_name;"

"Database=table_name;"

"Trusted_connection=yes;")

cur= db.cursor()

sql = """ exec databasename.dbo.sp_mystoredprc"""

cur.execute(sql)
(Mar-21-2018, 03:26 PM)PYTHONDUDE Wrote: [ -> ]i keep getting a error?
Please don't make me guess what the error is.
Please advise as to why this syntax will not work , in order for me to execute a sql stored proc within python.
also i would like to incorporate a error check, if the stored proc fails stop the entire process else continue until complete



import PYODBC



db = pyodbc.connect("Driver={SQL Server Native Client 11.0};"

"Server=server_name;"

"Database=table_name;"

"Trusted_connection=yes;")

cur= db.cursor()

sql1 = """ exec database_name.dbo.sp_mystoredproc""" #sql1 has to be ran before sql2 if it fails stop job

if sql1 == 0 then
print ('Your sp failed please check')
else

sql2 = """ select top 5 * from table name "

cur.execute(sql1)
cur.execute(sql2)
There's also no reason to double post :p