8

I'm recently getting the following BigQuery error when using the Python API:

google.api_core.exceptions.BadRequest: 400 configuration.query.destinationTable cannot be set for scripts

This is the function I use:

def execute_bigquery_sql(query, dataset_id, table_id, use_legacy_sql=True, write_disposition='WRITE_TRUNCATE'):
    client = bigquery.Client()
    job_config = bigquery.QueryJobConfig()
    job_config.use_legacy_sql = use_legacy_sql

    print("table_id: {table_id}".format(table_id=table_id))
    print("dataset_id: {dataset_id}".format(dataset_id=dataset_id))

    if table_id:
        table_ref = client.dataset(dataset_id).table(table_id)
        print("table_ref: {table_ref}".format(table_ref=table_ref))
        job_config.destination = table_ref
        job_config.write_disposition = write_disposition
        job_config.allow_large_results = True
        job_config.createDisposition = "CREATE_IF_NEEDED"

    query_job = client.query(query,job_config=job_config)
    results = query_job.result()  # Waits for job to complete.

Does anyone knows what might be happening and a workaround?

2 Answers 2

17

Thanks for the responses, comments were indeed in the right direction. In BigQuery scripting means https://cloud.google.com/bigquery/docs/reference/standard-sql/scripting.

And that is what is not allowed, and my query had:

DECLARE capital int64 default 10000000;

So, removing the line above was the fix in my case.

Interesting thing is that even in the web interface

  • If you use scripts the interface won't allow to save to table:

When using scripts such as DECLARE

  • In contrast, when not using script statements, you should see: Not using scripts
Sign up to request clarification or add additional context in comments.

3 Comments

my query settings look like the second screenshot where i am not able to use scripts. How do i change the query settings to use scripts like in the first screenshot?
Link doesn't work take to the meaning of scripting
What if I wanted to use declare statements. How to overcome it?
4

error is self-descriptive. scripts do not allow destination table to be set - instead you should use DML/DDL

workaround is to reset job_config with no destination table in it

5 Comments

Just a clarification, what does script mean in this case? How is this different from this official doc?
Ok, so the problem relies in the actual SQL in OP question, doesn't it? If that query didn't contain any scripting then it would have (at least under this aspect) worked, right?
when one runs script or DML/DDL - the destination is not allowed and will cause error
I'm running into a similar problem and don't quite understand the solution that OP explained: stackoverflow.com/questions/65590829/…

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.