pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
[GENERAL] Reset Sequence number
5+ messages / 5 participants
[nested] [flat]

* [GENERAL] Reset Sequence number
@ 2017-11-22 09:36  Brahmam Eswar <brahmam1234@gmail.com>
  0 siblings, 3 replies; 5+ messages in thread

From: Brahmam Eswar @ 2017-11-22 09:36 UTC (permalink / raw)
  To: pgsql-general

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.

^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: [GENERAL] Reset Sequence number
@ 2017-11-22 10:26  amul sul <sulamul@gmail.com>
  parent: Brahmam Eswar <brahmam1234@gmail.com>
  2 siblings, 1 reply; 5+ messages in thread

From: amul sul @ 2017-11-22 10:26 UTC (permalink / raw)
  To: Brahmam Eswar <brahmam1234@gmail.com>; +Cc: pgsql-general

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@gmail.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.




^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: [GENERAL] Reset Sequence number
@ 2017-11-22 10:56  Raymond O'Donnell <rod@iol.ie>
  parent: Brahmam Eswar <brahmam1234@gmail.com>
  2 siblings, 0 replies; 5+ messages in thread

From: Raymond O'Donnell @ 2017-11-22 10:56 UTC (permalink / raw)
  To: Brahmam Eswar <brahmam1234@gmail.com>; pgsql-general

On 22/11/17 09:36, Brahmam Eswar 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);

select max(id) into seq from table_1;

Ray.

-- 
Raymond O'Donnell :: Galway :: Ireland
rod@iol.ie




^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: [GENERAL] Reset Sequence number
@ 2017-11-22 11:00  Wèi Cōngruì <crvv.mail@gmail.com>
  parent: amul sul <sulamul@gmail.com>
  0 siblings, 0 replies; 5+ messages in thread

From: Wèi Cōngruì @ 2017-11-22 11:00 UTC (permalink / raw)
  To: amul sul <sulamul@gmail.com>; +Cc: Brahmam Eswar <brahmam1234@gmail.com>; 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@gmail.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@gmail.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.
>
>

^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: Reset Sequence number
@ 2017-11-22 11:04  Thomas Kellerer <spam_eater@gmx.net>
  parent: Brahmam Eswar <brahmam1234@gmail.com>
  2 siblings, 0 replies; 5+ messages in thread

From: Thomas Kellerer @ 2017-11-22 11:04 UTC (permalink / raw)
  To: pgsql-general

Brahmam Eswar schrieb am 22.11.2017 um 10:36:
> 
> 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"

No need for a PL/pgSQL block.

You can do that with a plain SQL statement using setval():

   select setval('table_1_seq', (select max(id) from table1));






^ permalink  raw  reply  [nested|flat] 5+ messages in thread


end of thread, other threads:[~2017-11-22 11:04 UTC | newest]

Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2017-11-22 09:36 [GENERAL] Reset Sequence number Brahmam Eswar <brahmam1234@gmail.com>
2017-11-22 10:26 ` amul sul <sulamul@gmail.com>
2017-11-22 11:00   ` Wèi Cōngruì <crvv.mail@gmail.com>
2017-11-22 10:56 ` Raymond O'Donnell <rod@iol.ie>
2017-11-22 11:04 ` Thomas Kellerer <spam_eater@gmx.net>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox