cape
cape

Reputation: 434

Change sequence start number

I'm trying to set my sequence to use current table max Id plus one as its next value but the following command fails

ALTER SEQUENCE newtable_id_sec START WITH ((select max(id) from newtable) +1);

Upvotes: 1

Views: 195

Answers (1)

user330315
user330315

Reputation:

DDL statements can't use sub-queries, but you can use setval() for this:

select setval('newtable_id_sec', ((select max(id) from newtable));

The +1 is not needed with setval() because setval() assumes the value that is passed is the last "used" value, so it will automatically continue with the next value.

Upvotes: 2

Related Questions