Find column names to insert into BigQuery

前端 未结 2 1325
囚心锁ツ
囚心锁ツ 2021-01-06 15:00

I am trying to do the \"insert into table\" and see that we need to explicitly specify the column names. Is there a way to get this data without having to manual type it out

2条回答
  •  無奈伤痛
    2021-01-06 15:20

    Here is an example to get the column names from a table (without incurring a cost) and build the INSERT list at the same time:

    WITH EmptyReference AS (
      SELECT *
      FROM `bigquery-public-data.samples.shakespeare`
      LIMIT 0
    )
    SELECT
      CONCAT(
        'INSERT dataset.tablename (',
        ARRAY_TO_STRING(
          REGEXP_EXTRACT_ALL(
            TO_JSON_STRING((SELECT AS STRUCT t.*)),
            r'"([^"]+)":'),
          ', '),
        ')')
    FROM (
      SELECT AS VALUE t
      FROM EmptyReference AS t
      UNION ALL SELECT AS VALUE NULL
    ) AS t
    

    This returns:

    INSERT dataset.tablename (word, word_count, corpus, corpus_date)     
    

    June 2019 Update

    Support for omitting column names in INSERT and MERGE statements is now in Beta.

    When the column names are omitted, all columns in the target table are included in ascending order based on their ordinal positions

    More details here

提交回复
热议问题