![]() |
execute a SQL stored proc within PYTHON - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: execute a SQL stored proc within PYTHON (/thread-9114.html) |
[temp] [PYTHON EXPORT SQL TO .txt file] - PYTHONDUDE - Mar-21-2018 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) RE: [PYTHON EXPORT SQL TO .txt file] - nilamo - Mar-21-2018 (Mar-21-2018, 03:26 PM)PYTHONDUDE Wrote: i keep getting a error?Please don't make me guess what the error is. execute a SQL stored proc within PYTHON - PYTHONDUDE - Mar-21-2018 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) RE: execute a SQL stored proc within PYTHON - nilamo - Mar-21-2018 There's also no reason to double post :p |