1

I have a dataframe that consists of one column of values and I want to pass it as a parameter to execute the following sql query:

query = "SELECT ValueDate, Value"\
        "FROM Table "\
        "WHERE [ID] in ( ? ) "

So I tried (among so many other things) the following:

      df = pd.read_sql_query(query, conn, params=[ df['ID'].values ])
      df = pd.read_sql_query(query, conn, params=[ df['ID'].tolist ])
      df = pd.read_sql_query(query, conn, params=[ list(df['ID'].values) ])
       ...

What is the correct way to pass the dataframe values ?

NB: I am using Microsoft SQL Server so the query needs to be formatted as I did.

1
  • Now that you have specified sql-server, table valued parameters might be the way to go. Commented Apr 11, 2017 at 19:18

2 Answers 2

4

Does this get you what you need?

import pandas as pd

your_column = pd.Series([1,2,3,4,5,6,7,8,9])

query = "SELECT ValueDate, Value"\
        "FROM Table "\
        "WHERE [ID] in {}".format(tuple(your_column))

print(query)
# 'SELECT ValueDate, ValueFROM Table WHERE [ID] in (1, 2, 3, 4, 5, 6, 7, 8, 9)'

Then you should be able to query without further parameters.

df = pd.read_sql_query(query, conn)
Sign up to request clarification or add additional context in comments.

1 Comment

This will work only if your column has more than one rows. If it has only one row, converting it into a tuple will result in SQL error. Query will look like " .... where [ID] in (1,) ". As you are converting the result into a tuple.
0
params = tuple(df['ID'].values)
sql = "SELECT COUNT(*) FROM foobar WHERE id IN (%s)" % (",".join(["?"]*len(params)),)
cursor.execute(sql, params)

2 Comments

Thank you but I forgot to say that I am using Microsoft SQL Server, so the query needs to be formatted as in my question.
Consider adding a description of how the code you've posted works, it will be helpful to users that visit this post in the future.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.