Subject Re: [firebird-support] ROW_NUMBER
Author Milan Babuskov
Lele Gaifax wrote:
>>Hi, is there a way to have a RowNumber for each row returned by a
>>query?

> The following stmt will bring the generator
> back to zero:
>
> SELECT gen_id(rownum, -gen_id(rownum, 0) FROM rdb$database

Why not simply: SET GENERATOR rownum TO 0;

Anyway, bad idea in multiuser environment.

> CREATE PROCEDURE CountingMyTable
> RETURNS (rownum INTEGER, fld1 SomeType, fld2 SomeOtherType) AS
> BEGIN
> rownum = 0;
> FOR SELECT mt.fld1, mt.fld2
> FROM MyTable mt
> ORDER BY 2,1
> INTO fld1, fld2 DO
> BEGIN
> rownum = rownum+1;
> SUSPEND;
> END
> END

If you use Firebird 2.0, you can do the same without stored procedure by
using EXECUTE BLOCK.

--
Milan Babuskov
http://www.guacosoft.com
http://www.flamerobin.org