eXPRESS
eXPRESS

Reputation: 465

PostgreSQL assign sequence number from SELECT

I want to restart sequence with max+1 of certain table.

SELECT max(id)+1 
    INTO testVal
FROM project;

ALTER SEQUENCE project_id_seq RESTART testVal;

This gives syntax error at testVal. Can someone please explain me what is a problem, propose alternative solution?

Upvotes: 0

Views: 85

Answers (1)

Lucas
Lucas

Reputation: 630

for sequences you should be using setval

SELECT SETVAL('project_id_seq', (SELECT max(id)+1 FROM project))

https://www.postgresql.org/docs/current/functions-sequence.html

Upvotes: 2

Related Questions