Stored Procedure Multiple Tables - PYODBC - Python

大城市里の小女人 提交于 2019-12-11 09:03:19

问题


I am trying to execute a stored Procedure with 20 different table outputs. These outputs range from 3-6 columns and 10-100 rows. If not pyodbc, how else would I be able to iterate through all these tables without the same structure?

connection = pyodbc.connect(r'DRIVER={SQL Server Native Client 
11.0};SERVER=dsdrsossql2;DATABASE=TableauDev;Trusted_Connection=yes;')
sql = "{call dbo.DGGrading}"
cur = connection.cursor()
rows = cur.execute(sql,).fetchall()

columns = [column[0] for column in cur.description]
df = pd.DataFrame.from_records(rows,columns=columns)
print(df)

回答1:


Consider using a list of dataframes and access multiple resultsets with nextset():

cur = connection.cursor()
df_list = []

# FIRST RESULTSET
rows = cur.execute(sql).fetchall()
columns = [column[0] for column in cur.description]
df_list.append(pd.DataFrame.from_records(rows, columns=columns))
print(df.head())

# SUBSEQUENT RESULTSETS
while (cur.nextset()): 
   columns = [column[0] for column in cur.description]
   df_list.append(pd.DataFrame.from_records(rows, columns=columns))
   print(df.head())

cur.close()

# RUN ANY DATAFRAME OPERATION BY EACH ITEM OF df_list 
df_list[[1]].describe()
df_list[[2]].head()
df_list[[3]].tail()
df_list[[4]].dtypes
df_list[[5]].columns



回答2:


Parfait's code didnt work for me because the 'subsequent resultset' was missing a line of code to pull the next table. I dont have enough points to comment, so here is the code that worked for me.

A few additional things

  • For MSSQL, CALL is not recognized so EXEC was used
  • Ensure SET NOCOUNT ON is in the stored procedure or fetchall wont pull in the tables correctly
param1 = "dynamic parameter1"
param2 = "dynamic parameter2"

sql = f"EXEC dbo.StoredProcedure '{param1}', '{param2}'"

cur = cnxn.cursor()
df_list = []

# get First result
rows = cur.execute(sql).fetchall()
columns = [column[0] for column in cur.description]
df_list.append(pd.DataFrame.from_records(rows, columns=columns))

# check for more results
while (cur.nextset()):
   rows = cur.fetchall()
   columns = [column[0] for column in cur.description]
   df_list.append(pd.DataFrame.from_records(rows, columns=columns))

cur.close()


来源:https://stackoverflow.com/questions/50683810/stored-procedure-multiple-tables-pyodbc-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!