Re: Adding auto-increment / sequence

From: "A(dot) Kretschmer" <andreas(dot)kretschmer(at)schollglas(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Adding auto-increment / sequence
Date: 2007-05-29 04:58:57
Message-ID: 20070529045857.GB19441@a-kretschmer.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

am Mon, dem 21.05.2007, um 18:29:46 -0700 mailte camb folgendes:
> Hey all,
> Is there any way to add some kind of sequence of auto-incrementing
> column to the result set returned by a SELECT?

Yes.

You can use an sequence similar this:

test=# select * from foo;
col1 | col2 | col3
------+------+------
1 | 2 |
2 | |
(2 rows)

test=*# create sequence foo_seq;
CREATE SEQUENCE
test=*# select nextval('foo_seq'), * from foo;
nextval | col1 | col2 | col3
---------+------+------+------
1 | 1 | 2 |
2 | 2 | |
(2 rows)

Don't forget to reset this sequence, the next select nextval() starts
with the current value and returns 3.

Other way: write an set-returning function and count the rows there.

Andreas
--
Andreas Kretschmer
Kontakt: Heynitz: 035242/47150, D1: 0160/7141639 (mehr: -> Header)
GnuPG-ID: 0x3FFF606C, privat 0x7F4584DA http://wwwkeys.de.pgp.net

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Robert James 2007-05-29 05:42:43 Will a DELETE violate an FK?
Previous Message A. Kretschmer 2007-05-29 04:53:22 Re: Call a program