1

I want to create a new table and assign the value of this table by writing a query.

from google.cloud import bigquery

bigquery_client = bigquery.Client(project="myproject")
dataset = bigquery_client.dataset("mydataset")
table_ref = dataset.table('New table')
table = bigquery.Table(table_ref)
table = client.create_table(table)

So, I created an empty table

And now I wish to have some values for this table from another table that's called "Old Table":

query = "SELECT * FROM `{Old table}`"

How can I make sure that my table is linked to this query?

I tried

table.view_query = "SELECT * FROM `{Old table}`" 

but it didn't work

Thanks,

1 Answer 1

2

I think you should use smth like this:

bigquery_client = bigquery.Client(project="myproject")
dataset = bigquery_client.dataset("mydataset")
table_ref = dataset.table('New_table')

sql_query = "SELECT * FROM `{project}.{dataset}.{table}`"

job_config = bigquery.QueryJobConfig()

# Set configuration.query.destinationTable
job_config.destination = table_ref

# Set configuration.query.createDisposition
job_config.create_disposition = 'CREATE_IF_NEEDED'

# Set configuration.query.writeDisposition
job_config.write_disposition = 'WRITE_APPEND'

# Start the query
job = bigquery_client.query(sql_query, job_config=job_config)

# Wait for the query to finish
job.result()
Sign up to request clarification or add additional context in comments.

4 Comments

thanks but when I ran job = bigquery_client.query(sql_query, job_config=job_config) it gives me a bunch of errors including Invalid table ID "New table"
Try "New_table" except "New table". Thats because IDs may only contain letters, numbers, and underscores.
Thanks, sorry, that was may bad. Still when I run job.result(). I got some errors such as in result raise self._exception and in result super(QueryJob, self).result(timeout=timeout). Do you have any idea, why?
Hm, i don't know whats an error. This code works perfectly for me. Try to update your google-cloud-bigquery with 'sudo pip install --upgrade google-cloud-bigquery'

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.