Unix Technical Forum

Adding auto-increment / sequence

This is a discussion on Adding auto-increment / sequence within the Pgsql General forums, part of the PostgreSQL category; --> Hey all, Is there any way to add some kind of sequence of auto-incrementing column to the result set ...


Go Back   Unix Technical Forum > Database Server Software > PostgreSQL > Pgsql General

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-09-2008, 05:21 PM
camb
 
Posts: n/a
Default Adding auto-increment / sequence

Hey all,
Is there any way to add some kind of sequence of auto-incrementing
column to the result set returned by a SELECT?

I need to get an auto incrementing column of some kind into the result
set from a view?

Thanks in advance,
Cam

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-09-2008, 05:31 PM
A. Kretschmer
 
Posts: n/a
Default Re: Adding auto-increment / sequence

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

---------------------------(end of broadcast)---------------------------
TIP 9: In versions below 8.0, the planner will ignore your desire to
choose an index scan if your joining column's datatypes do not
match

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 12:32 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com