I have a few SQL Queries stored in an Excel File.
I would like to run them on the given SQL database and then store results from the SQL query as well as the original data frame in a separate data frame.
from sqlalchemy import create_engine
import pymssql
engine = create_engine('connection string')
First I tried just storing results in a temp data frame which works fine. Each of these SQL queries give 3-4 rows of output.
df_result = pd.DataFrame()
for row in df.itertuples():
df_temp = pd.read_sql(row.SQL_Query, engine)
df_result = df_result.append(df_temp)
But the goal is store the result along with its associated query/row. The below is obviously wrong because the loop stores results from only last 3 times
df_result = pd.DataFrame()
for row in df.itertuples():
df_temp = pd.read_sql(row.SQL_Query, engine)
df_result = pd.merge(df, df_temp, left_index=True, right_index=True)