goofyui
goofyui

Reputation: 3492

How to select a last row - Column1 Value in SQL

How to select a last row - Column1 Value in SQL

I cant use Orderby .. As i dont have ID Column !!

Just want to pick most last row .. first column ..

select top 1 [FileName] from PlacedOrderDetails - But from Last Row ??

Upvotes: 0

Views: 2509

Answers (2)

Kelley
Kelley

Reputation: 11

This won't help with existing records, but if you want to track this from now on, you can add a column that automatically stores when the record was inserted:

ALTER TABLE PlacedOrderDetails ADD [DateTimeCreated] DateTime DEFAULT (GetDate())

Then just select the record with the most recent value for that column:

SELECT TOP 1 * FROM PlacedOrderDetails ORDER BY DateTimeCreated DESC

Upvotes: 1

Andomar
Andomar

Reputation: 238086

An SQL table is an unordered set. It does not, by default, contain information about the order in which records were created. So you have to explain to SQL Server what you mean by "first record". The way to do that is an order by clause.

If you just want any record, you can use a randomized order using the newid function:

select top 1 * from YourTable order by newid()

Upvotes: 3

Related Questions