-3

When i am trying to retrieve the last value from my data base it returns the first one. By using the following Query.

SELECT [Id] from dbo.Tb_Patient;

I want to get the last value. What query i should use?

rs.
  • 26,707
  • 12
  • 68
  • 90
  • Either `SELECT [Id] FROM [dbo].[Tb_Patient] ORDER BY [Id] DESC` or (better) look at http://stackoverflow.com/questions/7917695/sql-server-return-value-after-insert – Corak Mar 24 '13 at 15:23
  • ***last value*** ordered by what criteria? A SQL database (whatever product you're using) doesn't have any implicit order - you need to **define** the order you want by issuing `ORDER BY ......` in your query – marc_s Mar 24 '13 at 15:27

1 Answers1

1

Try this

SELECT Top(1) [Id] from dbo.Tb_Patient order by Id desc;

Alternatively if Id is Auto Incremented column then you can use MAX function as well to find Maximum value.

SELECT MAX([Id]) from dbo.Tb_Patient
Sachin
  • 40,216
  • 7
  • 90
  • 102