简体   繁体   中英

Variable table name in BigQuery Python Client

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM