4

I have a CTE like this:

WITH source1 as (
SELECT blah FROM blah
), source2 as (
SELECT moreblah FROM source1)

SELECT * FROM source2;

I want to insert the results of this query into a table, but when I write this:

WITH source1 as (
SELECT blah FROM blah
), source2 as (
SELECT moreblah FROM source1)

INSERT INTO newtable SELECT * FROM source2;

It says I have a syntax error Expected "(" or "," or keyword SELECT but got keyword INSERT. I'm wondering if this is a Bigquery issue b/c I've looked at other places like this which say my INSERT INTO should work. Any help would be appreciated!

Brian Guan
  • 193
  • 2
  • 12

1 Answers1

17

In BigQuery, the WITH goes with the SELECT:

INSERT INTO newtable 
    WITH source1 as (
          SELECT blah FROM blah
         ),
         source2 as (
          SELECT moreblah FROM source1
         )
    SELECT *
    FROM source2;
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786