Subject RE: [firebird-support] FIRST 1 question
Author Alan McDonald
In the database is
ATable with fields FIELD_PK, FIELD2, .... and FIELD_PK is the primary
key field.

In a stored procedure I use
select FIRST 1 FIELD_PK from ATABLE
where FIELD2 > 0

Can I be certain that this selection is the same as
select FIRST 1 FIELD_PK from ATABLE
where FIELD2 > 0
order by FIELD_PK?


NO. The SP select statement does not use an ORDER BY clause and therefore
NATURAL order will be used. That means that the record (s) will be retrieved
in order of where they are on the data page. Since records can be deleted
and their space taken up again by older records, an newer record (higher PK)
may well appear before an older record (lower PK).
Perhaps straight after a backup/restore cycle, you may see natural order the
same as PK order but there are no guarantees within the implementation.
That's why there is an ORDER BY clause in the SQL standard.
Alan