Skip to content Skip to sidebar Skip to footer

Pandas Io Sql And Stored Procedure With Multiple Result Sets

So I have a stored proc on a local sql server, this returns multiple data sets / tables Normally, in python / pyodbc I would use cursor.nextset() subset1 = cursor.fetchall() curso

Solution 1:

The following should work:

import pandas as pd
from sqlalchemy import create_engine

engine = create_engine('mysql://{}:{}@{}/{}'.format(username, password, server, database_name))
connection = engine.connect().connection
cursor = self.connection.cursor()

cursor.execute('call storedProcName(%s, %s, ...)', params)

# Results set 1
column_names = [col[0] for col in cursor.description] # Get column names from MySQL

df1_data = []
for row in cursor.fetchall():
    df1_data.append({name: row[i] for i, name inenumerate(column_names)})

# Results set 2
cursor.nextset()
column_names = [col[0] for col in cursor.description] # Get column names from MySQL

df2_data = []
for row in cursor.fetchall():
    df2_data.append({name: row[j] for j, name inenumerate(column_names)})

cursor.close()

df1 = pd.DataFrame(df1_data)
df2 = pd.DataFrame(df2_data)

Edit: I've updated the code here to avoid having to manually specify the column names.

Note that the original question only specifies a "local SQL server", not a specific kind of SQL server. This answer works with MySQL, but I haven't tested it with any other variety.

Post a Comment for "Pandas Io Sql And Stored Procedure With Multiple Result Sets"