-1

I need to convert a query from Oracle to Postgres which uses connect by level on a sequence in Oracle.

I know that connect by level is not available in Postgres. I couldn't find an alternate for this using recursive cte.

In Oracle if I run this query.

 create sequence ID_SEQ;
 select ID_SEQ.nextval from DUAL connect by level <= 3;

I will get the following result

1
2
3

I need the same in Postgres. Please share some solutions if anyone has any idea.

Thanks in advance Gokul.

4

1 Answer 1

4

The direct translation of that query is to use generate_series() and nextval()

select nextval('id_seq')
from generate_series(1,3);

This will advance the sequence three times.


If however the goal is to set a specific value for an existing sequence (which requires such a hack in Oracle), just use setval():

select setval('id_seq', 3);
Sign up to request clarification or add additional context in comments.

Comments

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.