Re: [GENERAL] Reset Sequence number

From: Wèi Cōngruì <crvv(dot)mail(at)gmail(dot)com>
To: amul sul <sulamul(at)gmail(dot)com>
Cc: Brahmam Eswar <brahmam1234(at)gmail(dot)com>, pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: [GENERAL] Reset Sequence number
Date: 2017-11-22 11:00:17
Message-ID: CAPxZtjH7Yumum7E2o9ySPe6cN4hXB7i02H+VOeJfZ5LHmqMRdw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

The 'SEQ' part can't be substituted by variable.
https://www.postgresql.org/docs/10/static/plpgsql-implementation.html

DO $$
DECLARE
SEQ BIGINT;
BEGIN
SEQ := (SELECT MAX(ID) FROM TABLE_1);
EXECUTE format('ALTER SEQUENCE TABLE_1_SEQ RESTART WITH %s', SEQ);
END
$$;

On Wed, Nov 22, 2017 at 6:26 PM, amul sul <sulamul(at)gmail(dot)com> wrote:

> Firstly, anonymous procedures are not supported in PostgreSQL, you need to
> embed this block in a plpgsql function[1] body & call that function if you
> want
> reset sequence value manually, or you could use CYCLE option[2] of a
> sequence to auto reset.
>
>
> 1] https://www.postgresql.org/docs/9.6/static/plpgsql-structure.html
> 2] https://www.postgresql.org/docs/devel/static/sql-createsequence.html
>
>
> Regards,
> Amul
>
> On Wed, Nov 22, 2017 at 3:06 PM, Brahmam Eswar <brahmam1234(at)gmail(dot)com>
> wrote:
> >
> > we are in process of migrating to postgres and need to reset the sequence
> > number with highest value of table key . I want to make it procedural to
> do
> > that as mentioned below,But it's throwing an error .
> >
> >
> >
> > DO $$
> > DECLARE
> > SEQ BIGINT;
> > BEGIN
> >
> > SEQ:=(SELECT MAX(ID) FROM TABLE_1);
> > ALTER SEQUENCE TABLE_1_SEQ RESTART WITH SEQ;
> >
> > END$$;
> >
> > Error : syntax error at or near "SEQ"
> >
> > --
> > Thanks & Regards,
> > Brahmeswara Rao J.
>
>

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Thomas Kellerer 2017-11-22 11:04:45 Re: Reset Sequence number
Previous Message Raymond O'Donnell 2017-11-22 10:56:30 Re: [GENERAL] Reset Sequence number