Subject Re: [firebird-support] Record numbering
Author Ivan Prenosil
> How to write select that gives field of selected record number. I have
> table just from one field "SomeField" how to get result:
>
>
> RecNo SomeField
> ----- --------------
> 1 sdferfer
> 2 ertertt dfgdfg
> 3 df ;dlfklee
> ...
>
> I do not want to use generators, I need every time to start from 1

Either do it on client.

Or do it using selectable stored procedure.

Or, if you use Firebird 2 and do not mind pretty ugly code, try this:


First initialize the counter:
SELECT RDB$SET_CONTEXT('USER_TRANSACTION', 'MyCounter', '0') FROM RDB$DATABASE

The do select like this (and just ignore the first column):
SELECT
RDB$SET_CONTEXT('USER_TRANSACTION', 'MyCounter', CAST(RDB$GET_CONTEXT('USER_TRANSACTION', 'MyCounter')AS INTEGER)+1),
CAST(RDB$GET_CONTEXT('USER_TRANSACTION', 'MyCounter') AS INTEGER),
RDB$COLLATION_NAME
FROM RDB$COLLATIONS
ORDER BY RDB$COLLATION_NAME;

RDB$SET_CONTEXT CAST RDB$COLLATION_NAME
=============== =========== ==========================
1 0 ASCII
1 1 BIG_5
1 2 BS_BA
1 3 CS_CZ
1 4 CYRL
1 5 DA_DA
1 6 DB_CSY
1 7 DB_DAN865
...


Ivan
http://www.volny.cz/iprenosil/interbase/