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

From: rolf(dot)ostvik(at)axxessit(dot)no
To: pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: SQL statement to set next serial value to max of a table?
Date: 2002-08-08 05:07:55
Message-ID: OFF0063962.06D676A8-ONC1256C0F.001B8289@axxessit.no
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Jean-Christian Imbeault <jc(at)mega-bucks(dot)co(dot)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

Browse pgsql-general by date

  From Date Subject
Next Message Peter Gibbs 2002-08-08 06:56:50 Re: transactions, serial ids, and JDBC
Previous Message Paul Ogden 2002-08-08 02:28:29 Re: transactions, serial ids, and JDBC