Python Forum
python call stored procedure with two parameter
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
python call stored procedure with two parameter
#1
Hi Team,

How to call below working stored procedure via python.

below stored procedure works in microsoft sql. when run manually.
EXEC dbo.unique_query @dbname= 'Database_India",@tablename = "States"


I have created cursor object, and attempted below code. no luck. plz assist.


params = ('ABC_Company','Employee")
sql = "Exec dbo.unique_query @dbname=?,@tablename=?"
#sql = "Exec unique_query @dbname=?,@tablename=?"

data = cursor.execute(sql,params)
cursor.commit()
print(data)
Reply
#2
Hi Team,

I found a solution with some troubleshooting , code started working.

params = ('ABC_Company','Employee")
sql = "Exec unique_query ?,?"


cursor.execute(sql,params)
data = cursor.fetchall()

print(data)
Reply
#3
cursor.fetchall() will return all responses to the "execute" as a list. For your particular need it might be valid to use curser.fetchone().

If there are multiple responses you could also use cursor.execute(sql, params) as an iterator.
for reply in cursor.execute(sql, params):
    print(reply)
This last is a lazy iterator. It retrieves replies one at a time. That can be useful for some kinds of queries.
Reply
#4
Hi Deanhystad,

Thanks for your help,

Can you help me I am using below piece of code, I need sql data with headers in it into csv files.
SQL Data size big one hence using csv modules.

to avoid memory issue , I am using fetchmany. is there anything to improve in my code.
cursor.execute('SELECT * FROM employee')
rows = cursor.fetchmany(10000)
import csv
cursor = connection.cursor()
cursor.execute('SELECT * FROM employee')
 
filename = "abc.csv"
 
 
# writing to csv file
with open(filename, 'w',newline = '') as csvfile:
    csvwriter = csv.writer(csvfile)
    csvwriter.writerow(col[0] for col in cursor.description)
    while True:
        rows = cursor.fetchmany(10000)
        print(rows)
        if len(rows) ==0:
            break
        else:
              for row in rows:
                 csvwriter.writerow(row)
    cursor.close()
    connection.close()
Reply
#5
I don't understand why you are writing the information to a file. Writing to a file and reading it back in to do whatever processing you plan to do feels like extra work with no return.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python script is hanging while calling a procedure in database prasanthi417 4 523 Jan-17-2024, 02:33 PM
Last Post: deanhystad
  How do I handle escape character in parameter arguments in Python? JKR 6 1,176 Sep-12-2023, 03:00 AM
Last Post: Apoed2023
  [ERROR] ParamValidationError: Parameter validation failed: Invalid type for parameter gdbengo 3 11,099 Dec-26-2022, 08:48 AM
Last Post: ibreeden
  python call stored procedure mg24 2 1,090 Oct-18-2022, 02:19 AM
Last Post: mg24
  Hi Guys, please help me to write SAS macro parameter equivalent code in Python Manohar9589 2 2,611 Jun-14-2020, 05:07 PM
Last Post: Larz60+
  I need my compiled Python Mac app to accept a file as a parameter Oethen 2 2,437 May-10-2020, 05:57 PM
Last Post: Oethen
  capture print statement written in Stored Procedure in SQL Server brijeshkumar_77 0 2,567 Feb-18-2020, 03:22 AM
Last Post: brijeshkumar_77
  Execute SQL Server Stored Procedure brijeshkumar_77 0 3,051 Feb-18-2020, 02:59 AM
Last Post: brijeshkumar_77
  what would you call the input for the parameter(s) of a function you have defined? rix 3 2,455 Dec-16-2019, 12:04 AM
Last Post: rix
  Python Parameter inside Json file treated as String dhiliptcs 0 1,851 Dec-10-2019, 07:28 PM
Last Post: dhiliptcs

Forum Jump:

User Panel Messages

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