Alessandro Cosentino
Alessandro Cosentino

Reputation: 2364

Write from a query to table in BigQuery only if query is not empty

In BigQuery it's possible to write to a new table the results of a query. I'd like the table to be created only whenever the query returns at least one row. Basically I don't want to end up creating empty table. I can't find an option to do that. (I am using the Python library, but I suppose the same applies to the raw API)

Upvotes: 0

Views: 1354

Answers (2)

Jeremy Condit
Jeremy Condit

Reputation: 7046

There's no option to do this in one step. I'd recommend running the query, inspecting the results, and then performing a table copy with WRITE_TRUNCATE to commit the results to the final location if the intermediate output contains at least one row.

Upvotes: 1

Bmo
Bmo

Reputation: 1212

Since you have to specify the destination on the query definition and you don't know what it will return when you run it can you tack a LIMIT 1 to the end?

You can check the row number in the job result object and then re run the query without the limiter if there are results into your new table.

Upvotes: 2

Related Questions