pyodbc/sqlalchemy - read each column in the table using pd.read_sql_query. Pass variable through the query
<p>I want to pass a variable 'single_column' through pd.read_sql_query in loop:</p>

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

<p>I tried to use something like this:</p>

<pre><code>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})
</code></pre>

<p>No luck at all!</p>

<p>You can't "paremeterize" table or column names in SQL (SQL allows to "parameterize" <a href="https://en.wikipedia.org/wiki/Literal_(computer_programming)" rel="nofollow">literals</a> only), but you can easily do it on Python level:</p>

<pre><code>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'
</code></pre>

<p>or in your case:</p>

<pre><code>df_trial_queries = pd.read_sql_query("SELECT {} FROM dw.db.table".format(single_column), db_cnxn)
</code></pre>

<p>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...</p>

python sql pandas sqlalchemy pyodbc