Python Forum
How to capture the result set from a stored procedure?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to capture the result set from a stored procedure?
#1
Hello,
When I ran the following code, it got the message "No results. Previous SQL was not a query." How do I get the dataset returned from the stored procedure? Thank you.
conn_str = ('DRIVER={SQL Server};SERVER=' + server + ';DATABASE=' + database + ';Trusted_Connection=yes;')
cnxn = pyodbc.connect(conn_str)

cursor = cnxn.cursor()
sp = "EXEC spTest"
dataset = cursor.execute(sp)
df_Test = dataset.fetchall()
print(df_Test)
deanhystad write May-24-2024, 04:19 PM:
No BBcode Answer
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button
Reply
#2
Read the docs. Always good advice.

https://learn.microsoft.com/en-us/sql/re...rver-ver16

In this case the info is right near the top.
DECLARE @return_value int
 
EXEC    @return_value = [dbo].[GetCustomerEmail]
        @CustID = 10
 
SELECT  'Return Value' = @return_value
You need to declare a variable. Call the procedure, telling the procedure to put results in the variable. Use the variable to retrieve the results.
Reply
#3
Thanks deanhystad,

Those are SQL code. How do I get the dataset in Python? Your example is returning a single value in SQL or Python? It seems in SQL.

I tried the link you provided for the Examples of returning data using a result set. I added RETURN at the end of the stored procedure, but I got the same message "No results. Previous SQL was not a query."

In Python, I just use dateset = the SQL return value (or dataset)?

Thanks again.
Dee
Reply
#4
More read the docs? This is pyodbc specific.

https://github.com/mkleehammer/pyodbc/wi...Procedures
dee likes this post
Reply
#5
Thanks again deanhystad,

That works. It returned the dataset. Now it needs to be converted to a data frame like a table row and column layout. Do you have more reading for me? Smile

I tried pd.DataFrame(rows) but all columns combined in one long column.
Reply
#6
Read the string like it is a CSV file. Read about pandas.read_csv() and io.StringIO(str).
dee likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python script is hanging while calling a procedure in database prasanthi417 4 741 Jan-17-2024, 02:33 PM
Last Post: deanhystad
  python call stored procedure mg24 2 1,186 Oct-18-2022, 02:19 AM
Last Post: mg24
  python call stored procedure with two parameter mg24 4 1,789 Sep-27-2022, 05:02 AM
Last Post: deanhystad
  capture print statement written in Stored Procedure in SQL Server brijeshkumar_77 0 2,668 Feb-18-2020, 03:22 AM
Last Post: brijeshkumar_77
  Execute SQL Server Stored Procedure brijeshkumar_77 0 3,209 Feb-18-2020, 02:59 AM
Last Post: brijeshkumar_77
  What do you think of this procedure to create a path to a folder atlass218 10 6,706 Sep-06-2019, 07:53 AM
Last Post: atlass218
  How to return value from a stored procedure of a MySQL database? UtiliseIT 0 2,403 Jul-22-2019, 10:14 AM
Last Post: UtiliseIT
  Add a stored procedure with variable using MySQL Connector UtiliseIT 0 2,344 May-04-2019, 12:46 PM
Last Post: UtiliseIT
  Unable to use sqlanydb to execute Sybase IQ Stored Procedure FORTITUDE 4 3,918 Sep-15-2018, 05:03 PM
Last Post: FORTITUDE
  Procedure Not Executing jge047 1 2,791 Dec-01-2017, 07:00 AM
Last Post: buran

Forum Jump:

User Panel Messages

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