Re: Adding auto-increment / sequence
| От | A. Kretschmer | 
|---|---|
| Тема | Re: Adding auto-increment / sequence | 
| Дата | |
| Msg-id | 20070529045857.GB19441@a-kretschmer.de обсуждение исходный текст | 
| Ответ на | Adding auto-increment / sequence (camb <mvpaustralia@gmail.com>) | 
| Список | 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
		
	В списке pgsql-general по дате отправления: