3

This query generates the numbers from 1 to 4.

with recursive z(q) as (
  select 1
  union all
  select q + 1 from z where q < 4
  )
select * from z;

But, if I modify it to this,

with x as (
  select 1 y
  ),
recursive z(q) as (
  select y from x
  union all
  select q + 1 from z where q < 4
  )
select * from z;

It gives

ERROR: syntax error at or near "z"

What did i do wrong here?

1

1 Answer 1

4

I think this is because RECURSIVE is modifier of WITH statement, not a property of common table expression z, so you can use it like this:

with recursive
x as (
  select 1 y
),
z(q) as (
  select y from x
  union all
  select q + 1 from z where q < 4
)
select * from z;

sql fiddle demo

Sign up to request clarification or add additional context in comments.

1 Comment

Recursive enables self-referencing queries in any of the list of multiple CTEs, not just the first.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.