Python Forum
execute a SQL stored proc within PYTHON
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
execute a SQL stored proc within PYTHON
#1
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)
Reply
#2
(Mar-21-2018, 03:26 PM)PYTHONDUDE Wrote: i keep getting a error?
Please don't make me guess what the error is.
Reply
#3
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)
Reply
#4
There's also no reason to double post :p
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Generate lists of devices and partitions from /proc/partitions? DachshundDigital 1 765 Feb-28-2023, 10:55 PM
Last Post: deanhystad
  python call stored procedure mg24 2 1,069 Oct-18-2022, 02:19 AM
Last Post: mg24
  python call stored procedure with two parameter mg24 4 1,493 Sep-27-2022, 05:02 AM
Last Post: deanhystad
  How To Set Up And Execute A Python File knight2000 4 1,892 May-31-2022, 10:02 AM
Last Post: Larz60+
  execute python script guy7200 1 1,590 Oct-25-2021, 09:55 PM
Last Post: Axel_Erfurt
  Possible to execute a python script before log off/shutdown with input commands? Kaltex 1 2,259 May-18-2021, 06:31 AM
Last Post: Skaperen
  Using python to execute app or cmd that requires admin credentials thewolf 0 2,103 Mar-05-2021, 08:15 PM
Last Post: thewolf
  Execute DBCC CHECKDB from python script susja 1 2,102 Aug-12-2020, 02:09 AM
Last Post: susja
  Trying to write and execute first Python script garvind25 3 2,890 May-23-2020, 07:36 PM
Last Post: garvind25
  Execute SQL Server Stored Procedure brijeshkumar_77 0 3,033 Feb-18-2020, 02:59 AM
Last Post: brijeshkumar_77

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020