Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 519
Dinesh Avatar asked Aug 31 '25 20:08

Dinesh


2 Answers

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';
like image 167
Gordon Linoff Avatar answered Sep 03 '25 14:09

Gordon Linoff


Consider changing address Mode to REPEATED:

enter image description here

Also as name needs to be removed:

INSERT  INTO `my_project.my_dataset.test_table`(name, address, comments)
values('my_name',
       [STRUCT('ABC','XYZ',10), STRUCT('PQR','STU',20)],
       'Comment'
      )
like image 36
Sergey Geron Avatar answered Sep 03 '25 14:09

Sergey Geron