user735627
user735627

Reputation: 281

Get last 10 records in SQL Server 2005

Please can any one help me in writing a query.

I want to get the last inserted 10 records from a table.

Upvotes: 2

Views: 7151

Answers (3)

GSerg
GSerg

Reputation: 78185

Unless you have a field in your table declared like this:

date_putin datetime not null default getdate()

and unless this field can never be written to by a client, you can't. Because there's no row order in a table.

But if you do have this field, and no client can ever write to it, then

select top (10) * from t order by date_putin desc;

Upvotes: 1

The Evil Greebo
The Evil Greebo

Reputation: 7138

Assuming your table has a primary key which is an identity column:

Select top 10 * from mytable order by mytable.id desc

Upvotes: 3

Lourens
Lourens

Reputation: 1518

It would be something like

SELECT TOP 10 * FROM MyTable ORDER BY MyID DESC

MyID should be the primary key

Upvotes: 3

Related Questions