Wednesday, March 21, 2012

last row

Hi!

Another problem:

How can I fetch only the last row in my table?

Thanks,
Darko"Darko Jovisic" <djovisic@.fesb.hr> wrote in message
news:c92khn$spa$1@.bagan.srce.hr...
> Hi!
> Another problem:
> How can I fetch only the last row in my table?
> Thanks,
> Darko

How do you define 'last'? Rows in a table are not in any order, so you have
to use something in the data which means 'last' to you - perhaps a
CreatedDate column, or an OrderNumber, or something else:

select top 1 *
from dbo.MyTable
order by CreatedDate desc

select top 1 *
from dbo.MyTable
order by OrderNumber desc

Simon|||I understand now how to solve the problem!
Thank you very much!

"Simon Hayes" <sql@.hayes.ch> wrote in message
news:40b4d99d_3@.news.bluewin.ch...

> How do you define 'last'? Rows in a table are not in any order, so you
have
> to use something in the data which means 'last' to you - perhaps a
> CreatedDate column, or an OrderNumber, or something else:
> select top 1 *
> from dbo.MyTable
> order by CreatedDate desc
> select top 1 *
> from dbo.MyTable
> order by OrderNumber desc
> Simon|||>> How can I fetch only the last row in my table? <<

Let's get back to the basics of an RDBMS; there is no sequential
access or ordering in an RDBMS, so "first", "next" and "last" are
totally meaningless.

No comments:

Post a Comment