0

I have following PostgreSQL create script

CREATE SEQUENCE seq_tbl_ct_yr2_id  START (select max(ct_tran_id)+1 tranid from tbl_ct);

this doestnt create sequence arising folloeing error:

ERROR: syntax error at or near "(" LINE 1: create sequence test_1 start (select 1)

for test purpose i tested following scripts

create sequence test start 1 -- this works

create sequence test_1 start (select 1) -- this doesnt work 

how to overcome this ??

Note : PostgreSQL 9.2

2 Answers 2

3

You can't specify the start value as a sub-select. However you can set the sequence to a specific value using setval()

CREATE SEQUENCE seq_tbl_ct_yr2_id;
select setval('seq_tbl_ct_yr2_id', (select max(ct_tran_id)+1 tranid from  tbl_ct));
Sign up to request clarification or add additional context in comments.

Comments

0
DO
$$
declare
start_id int;
begin
select max(ct_tran_id)+1 from tbl_ct  into start_id;
execute 'CREATE SEQUENCE seq_tbl_ct_yr2_id  START '||start_id||'';
end;
$$

test using

select nextval('seq_tbl_ct_yr2_id')

sql-do

Comments

Your Answer

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