Subject Re: AW: [firebird-support] Generator
Author Mark Rotteveel
On Tue, 8 May 2012 15:09:34 +0200, "Olaf Kluge" <olaf.kluge@...>
wrote:
> my description was not good.
>
> I need the value in a stored procedure, not by inserting the data! By
> inserting I have a trigger. new.id = gen_id(generator,1);
>
> I have a table with some records. Now I need the last 10 inserted
records
> with an offset of 5 for example. The last ID is 100, I want to get the
> records (IDs) 95 to 86 in this case. If the offset is 10, I need the
> records
> 90 to 81 and so on.
>
> To get the last ID I thought it is the best way to use the generator
with
> the value 0, but the description warns about errors.

That warning is about assuming that the number represents the last record
*you* inserted. However. If you want to retrieve the last 10 records it is
still a bad idea to use the generator, because if records were inserted
after your transaction started, then you will see less than 10 records.

It would probably be better to do
SELECT <fields you want> FROM <table> ORDER BY ID DESC ROWS 10

As that will guarantee you get the last 10 records inserted that are
visible to your transaction.