Hello Team
How can i get a value from a column in the last row
for ex: select orderno from orders .......... but the last row ?
Thanks lot
Greetings,
SQL Server has no concept of last row, or first row or middle row for that matter.
To determine the last row you need to know by what criteria. If your Order Number column is sequential or your table contains a datetime column then you can get a close approximation by using TOP and ORDER BY.
SELECT TOP 1 OrderNo FROM Orders ORDER BY OrderDate DESC
If there is no column in the table from which you can determine the correct ordering then you're stuck.
--
Regards,
Neale NOON
There is one more work around available to get the last record..
if your table has 3 columns,
declare @.col1 as varchar(100);
declare @.col2 as varchar(100);
declare @.col3 as varchar(100);
Select @.Co1l=Col1,
@.Col2=Col2,
@.Col3=Col3
From
Tablename;
Select @.Col1 as Col1, @.Col2 as Col2, @.Col3 as Col3;
So here you need not to use any order by clause. It always fet the last row.
|||If you are really just looking for the last row, reverse your sort order and use SELECT TOP 1 ... That will get you the first row in the reversed order, ie last row.
No comments:
Post a Comment