Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Using values inside a cte in snowflake queries

I’m trying to use values inside a cte in snowflake. For example, this:

WITH person (id, name)
     AS (VALUES (1, 'Ted'), (2, 'Bill'))
SELECT *
FROM   person; 

works fine in (for example) postgres, but gives me the following error in snowflake:

SQL compilation error: syntax error line 2 at position 9 unexpected 'VALUES'. syntax error line 2 at position 17 unexpected '1'. syntax error line 2 at position 26 unexpected ','.

However, from snowflake documentation I can see VALUES syntax is supported, for example this works in snowflake:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

SELECT * FROM (VALUES (1, 'Ted'), (2, 'Bill')) as person (id, name);

So I’m wondering how I’d get it to work inside a cte.

I would like the same output as I would get from postgres (using a cte)

 id | name 
----+------
  1 | Ted
  2 | Bill
(2 rows)

>Solution :

You have to add a SELECT statement inside your CTE

WITH person AS (
    SELECT $1 AS id,
           $2 AS name
    FROM (VALUES (1, 'Ted'), (2, 'Bill'))
)
SELECT *
FROM   person; 

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading