Reputation: 5946
Having seen the correct format for the setting of variables for sql queries, I'm not finding a similar way to parameterise the table name. For example, I have a simple query as follows
query = """
select price, category, title, sm_title from `product.data_set.table_name` where
product = @product"""
job_config = bigquery.QueryJobConfig(
query_parameters=[
bigquery.ScalarQueryParameter("product", "INT64", product),
])
product_data = client.query(query, job_config=job_config).to_dataframe()
I would like the table_name
to be a variable that can be passed in. Is there can option that can be used like is set in the job_config?
Upvotes: 0
Views: 2677
Reputation: 2954
Setting of python variable for SQL query can be done using python's f string formatting approach as suggested by @EdoAkse in the comments.
You may use below
table_name = "tablenamegoeshere"
query = f""" select price, category, title, sm_title from `product.data_set.{table_name}` where product = @product"""
Posting the answer as community wiki for the benefit of the community that might encounter this use case in the future.
Feel free to edit this answer for additional information.
Upvotes: 1