SQL statement to set next serial value to max of a table?

Started by Jean-Christian Imbeaultover 23 years ago2 messagesgeneral
Jump to latest
#1Jean-Christian Imbeault
jc@mega-bucks.co.jp

I want to execute the following two pseudo-SQL statements (that set the
current value in a serial as the max of the primary key) as one SQL
statement. What is the proper syntax?

1- MAX = select max(id) from T;
2- SELECT setval('T_id_seq', MAX);

Jc

#2Noname
rolf.ostvik@axxessit.no
In reply to: Jean-Christian Imbeault (#1)
Re: SQL statement to set next serial value to max of a table?

Jean-Christian Imbeault <jc@mega-bucks.co.jp> wrote:

I want to execute the following two pseudo-SQL statements (that set the
current value in a serial as the max of the primary key) as one SQL
statement. What is the proper syntax?

1- MAX = select max(id) from T;
2- SELECT setval('T_id_seq', MAX);

either
SELECT setval( 'T_id_seq' , max(id) ) from T;
or
SELECT setval( 'T_id_seq' , (select max(id) from T) );
should do it

--
Rolf