Обсуждение: reading last inserted record withoud any autoincrement field

Поиск
Список
Период
Сортировка

reading last inserted record withoud any autoincrement field

От
mohammad qoreishy
Дата:
<table border="0" cellpadding="0" cellspacing="0"><tr><td style="font: inherit;" valign="top">How can get last inserted
recordin a table without any autoincrement filed?<br />I need to  frequently fetch the last inserted record.<br />If I
mustuse the "Cursor" please explain your solution.<br /></td></tr></table><br /> 

Re: reading last inserted record withoud any autoincrement field

От
Osvaldo Kussama
Дата:
2009/10/4 mohammad qoreishy <m_qoreishy@yahoo.com>
>
> How can get last inserted record in a table without any autoincrement filed?
> I need to  frequently fetch the last inserted record.
> If I must use the "Cursor" please explain your solution.
>


RETURNING clause?
http://www.postgresql.org/docs/current/interactive/sql-insert.html

Osvaldo


Re: reading last inserted record withoud any autoincrement field

От
Rob Sargent
Дата:
Osvaldo Kussama wrote:
> 2009/10/4 mohammad qoreishy <m_qoreishy@yahoo.com>
>   
>> How can get last inserted record in a table without any autoincrement filed?
>> I need to  frequently fetch the last inserted record.
>> If I must use the "Cursor" please explain your solution.
>>
>>     
>
>
> RETURNING clause?
> http://www.postgresql.org/docs/current/interactive/sql-insert.html
>
> Osvaldo
>
>   
It took the OP to mean last insert as in randomly in the past, not as 
part of current transaction.  My fear is OP's schema has no way of 
identifying time-of-insert, nor a monotonically increasing record id and 
is hoping postgres has a some internal value that will return the most 
recently inserted record. Without a table definition it's hard to say.


Re: reading last inserted record withoud any autoincrement field

От
Scott Marlowe
Дата:
On Sun, Oct 4, 2009 at 1:34 PM, Rob Sargent <robjsargent@gmail.com> wrote:
> Osvaldo Kussama wrote:
>>
>> 2009/10/4 mohammad qoreishy <m_qoreishy@yahoo.com>
>>
>>>
>>> How can get last inserted record in a table without any autoincrement
>>> filed?
>>> I need to  frequently fetch the last inserted record.
>>> If I must use the "Cursor" please explain your solution.
>>>
>>>
>>
>>
>> RETURNING clause?
>> http://www.postgresql.org/docs/current/interactive/sql-insert.html
>>
>> Osvaldo
>>
>>
>
> It took the OP to mean last insert as in randomly in the past, not as part
> of current transaction.  My fear is OP's schema has no way of identifying
> time-of-insert, nor a monotonically increasing record id and is hoping
> postgres has a some internal value that will return the most recently
> inserted record. Without a table definition it's hard to say.

Given that he's mentioning cursors, I'm guessing he's talking about
during this session / transaction.  So returning would be best.  Note
that returning returns a SET of results, so that if your insert
inserts 10 rows, you'll get back 10 rows from returning.