xixi wrote:
hi, we are running db2 udb on v8.1, i am using jdbc sql, is there
anyway to create a sql saying i want to get the last record of
resultset? i try to open a big resultset and point the cursor to the
last (like select * from table, then do resultset.last), i only want
to get a one row back which is the last row of the resultset. thanks
I have always been confused by people wanting to do things like this,
because DB2 uses the relational model wherein the data are a set, not an
ordered set. So the idea of a "last row" is meaningless. So it escapes me
why people go out of their way to impose an ordering without defining the
ordering.
As a practical matter, it seems that DB2 would return rows in response to
a query (that does not have ORDER BY) in about the order they were entered
(if no reorganization has taken place) or in the order in which they were
presently stored. But it does not matter much which, as far as I can tell.
I am not saying it is not a possible solution to some problem or other.
But it seems to me that since the concept of "last row" is at best
undefined, it would seem to make more sense to define what is wanted and
retrieve that.
So if you want a "last row", why not do an ORDER BY ... and arrange for
the "last row" to come out first. You could even have the query contain
stuff like:
ORDER BY column1 DESC, column2 ASC ...
FOR READ ONLY
OPTIMIZE FOR 1 ROWS;
or something like that. If there is no such column or columns, put one in,
if only temporarily.
--
.~. Jean-David Beyer Registered Linux User 85642.
/V\ Registered Machine 241939.
/( )\ Shrewsbury, New Jersey
http://counter.li.org
^^-^^ 09:15:00 up 4 days, 16:30, 3 users, load average: 3.47, 3.94, 4.03