I'm using Pyodbc in Python to run some SQL queries. What I'm working with is actually longer than this, but this example captures what I'm trying to do:
connection = pyodbc.connect(...)
cursor = connection.cursor(...)
dte = '2018-10-24'
#note the placeholders '{}'
query = """select invoice_id
into #output
from table1 with (nolock)
where system_id = 'PrimaryColor'
and posting_date = '{}'
insert into #output
select invoice_id
from table2 with (nolock)
where system_id = 'PrimaryColor'
and posting_date = '{}'"""
#this is where I need help as explained below
cursor.execute(query.format(dte, dte))
output = pd.read_sql("""select *
from #output"""
, connection)
In the above, since there are only two '{}'
, I'm passing dte
to query.format()
twice. However, in the more complicated version I'm working with, I have 19 '{}'
, so I'd imagine this means I need to pass 'dte'
to 'query.format{}'
19 times. I tried passing this as a list, but it didn't work. Do I really need to write out the variable 19 times when passing it to the function?