Search code examples
sqlstructgoogle-bigqueryinsertnested-table

BigQuery Insert into Nested records


I have a table that has a nested record (please see attached snapshot).

schema defination

I am trying to do a simple insert but it's not working.

INSERT  INTO `my_project.my_dataset.test_table`(name,address,comments)
values( 
'my_name' as name,
  [STRUCT('ABC' as line1,'XYZ' as line2,10 as code),
   STRUCT('PQR' as line1,'STU' as line2,20 as code)],
   'Comment') 

Any idea what's wrong with this SQL insert statement?

Thanks a lot for your help.


Solution

  • Don't assign names in values. Try:

    INSERT  INTO `my_project.my_dataset.test_table`(name,address,comments)
        values('my_name',
               [STRUCT('ABC' as line1,'XYZ' as line2,10 as code),
                STRUCT('PQR' as line1,'STU' as line2,20 as code)],
               'Comment'
              ) 
    

    Or use insert . . . select:

    insert into `my_project.my_dataset.test_table`(name,address,comments)
        select 'my_name' as name,
               [STRUCT('ABC' as line1,'XYZ' as line2,10 as code),
                STRUCT('PQR' as line1,'STU' as line2,20 as code)],
               'Comment';