Truncate a table in GBQ

前端 未结 4 829
粉色の甜心
粉色の甜心 2021-02-02 11:21

I am trying to truncate an existing table in GBQ but the below command fails when I run it. Is there any specific command or syntax to do that. I looked into GBQ documentation b

相关标签:
4条回答
  • 2021-02-02 11:42

    Good news, TRUNCATE TABLE is supported by now: https://cloud.google.com/bigquery/docs/reference/standard-sql/dml-syntax#truncate_table_statement

    TRUNCATE TABLE [[project_name.]dataset_name.]table_name
    

    However, please note that this will not work / is not supported, if a partition filter is required through your table definition.

    0 讨论(0)
  • 2021-02-02 11:57

    EDIT (Nov 2020): BigQuery now supports other verbs, check other answers for newer solutions.

    BigQuery doesn't support TRUNCATE as part of a query string. The only DDL/DML verb that BQ supports is SELECT.

    One option is to run a job with WRITE_TRUNCATE write disposition (link is for the query job parameter, but it's supported on all job types with a destination table). This will truncate all data already in the table and replace it with the results of the job.

    If you don't want to replace the contents with other data or start a job, your best option is probably to delete and recreate the table with the same schema.

    0 讨论(0)
  • 2021-02-02 11:57

    While BigQuery didn't used to support anything other than SELECTs, it now does as long as you uncheck "Use Legacy SQL" in the query options. There is no truncation, but you can delete:

    DELETE from my_table WHERE 1=1
    

    Note that BigQuery requires the use of WHERE in the DELETE, so if you want to delete everything you need to use a statement that will always be true.

    0 讨论(0)
  • 2021-02-02 12:09
    CREATE OR REPLACE TABLE <dataset>.<table>
    AS SELECT * FROM <dataset>.<table> LIMIT 0;
    

    For partitionned tables, assuming you have a day partition on field "created_on", then execute the following :

    CREATE OR REPLACE TABLE <dataset>.<table> PARTITION BY created_on
    AS SELECT * FROM <dataset>.<table> WHERE created_on = CURRENT_DATE() LIMIT 0;
    
    0 讨论(0)
提交回复
热议问题