Re: How can I make SELECT return tuple numbers?

Поиск
Список
Период
Сортировка
От Michael Fuhr
Тема Re: How can I make SELECT return tuple numbers?
Дата
Msg-id 20050706035548.GA1408@winnie.fuhr.org
обсуждение исходный текст
Ответ на How can I make SELECT return tuple numbers?  (Roy Souther <roy@SiliconTao.com>)
Ответы Re: How can I make SELECT return tuple numbers?  (Michael Fuhr <mike@fuhr.org>)
Список pgsql-general
On Tue, Jul 05, 2005 at 09:34:51PM -0600, Roy Souther wrote:
>
> Is there any way to make a SELECT return an extra field that is a
> sequential number for the tuples that are returned?

You could use a sequence.

> I am trying to populate a table with an INSERT SELECT and would like to
> make one of the fields a sequence starting with 1 for the first tuple
> and incromenting. I don't want the field to be a unique index. As the
> query is run again and again I want more tuples appended to the table
> and each time the counter should start at 1 and count up.

You could reset a sequence's start value before each insert.  You
might even be able to it automagically with a statement-level before
trigger that resets the sequence value and a DEFAULT expression or
row-level trigger that uses nextval() to obtain the next sequence
value.  That appears to work in simple tests, although I admittedly
haven't thought it through very far to see if there would be any
problems.

--
Michael Fuhr
http://www.fuhr.org/~mfuhr/

В списке pgsql-general по дате отправления:

Предыдущее
От: Roy Souther
Дата:
Сообщение: How can I make SELECT return tuple numbers?
Следующее
От: Michael Fuhr
Дата:
Сообщение: Re: How can I make SELECT return tuple numbers?