dsl1990 dsl1990 - 3 months ago 26
SQL Question

pyodbc/sqlalchemy - read each column in the table using pd.read_sql_query. Pass variable through the query

I want to pass a variable 'single_column' through pd.read_sql_query in loop:

for single_column in columns_list:
df_trial_queries = pd.read_sql_query("SELECT single_column FROM dw.db.table;",db_cnxn)


I tried to use something like this:

for single_column in columns_list:
df_trial_queries = pd.read_sql_query("SELECT %(column_name)s FROM dw.db.table;",db_cnxn,params = {'column_name':single_column})


No luck at all!

Answer

You can't "paremeterize" table or column names in SQL (SQL allows to "parameterize" literals only), but you can easily do it on Python level:

In [25]: single_column = 'col1'

In [52]: table = 'dw.db.table'

In [53]: "SELECT {} FROM {}".format(single_column, table)
Out[53]: 'SELECT col1 FROM dw.db.table'

or in your case:

df_trial_queries = pd.read_sql_query("SELECT {} FROM dw.db.table".format(single_column), db_cnxn)

NOTE: it's very inefficient way! I'm sure there is a better way to achieve your goals, but you would have to shed some light on what are you going to achieve using this loop...

Comments