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 ??
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()
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
精彩评论