Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bigquery INSERT after WITH AS statement not working

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!

like image 246
Brian Guan Avatar asked May 06 '26 18:05

Brian Guan


1 Answers

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;
like image 191
Gordon Linoff Avatar answered May 09 '26 09:05

Gordon Linoff



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!