0

How to set value to sequence in plpgsql ?

This fails in plpgsql

select setval('public.student_id', select max(student_id) from public.student);
eshirvana
  • 23,227
  • 3
  • 22
  • 38
cofactor
  • 13
  • 6
  • @LaurenzAlbe, you are correct, it is possible to use a subquery. As you point out in your answer it just needs to be surrounded by parentheses, so: `select setval('public.student_id', (select max(student_id) from public.student));` – Adrian Klaver May 10 '21 at 14:54

2 Answers2

0

you can do this instead, imagining public.student_id is the name of sequence that already exists:

select setval('public.student_id',max(student_id))
from public.student;
eshirvana
  • 23,227
  • 3
  • 22
  • 38
0

All you have to do is surround the subquery with parentheses. That is syntactically required.

Laurenz Albe
  • 209,280
  • 17
  • 206
  • 263