Tuesday, February 21, 2012

Returning last row only

I have a web application that is data driven from a SQL 2005 database. The application needs to prompt the user to enter some information that will be logged into a SQL table.

It should always be the last row in the table that is being worked on at any one time. Over a period the user will need to enter various fields. Once the data is entered into a field they will not have access to amend it.

Therefore I need to be able to SELECT the last row of a table and present the data to the user with the 'next field' to be edited.

As I'd like to do this as a stored procedure which can be called from an ASP page I wonder if anyoen might be able to help me with some T-SQL code that might achieve it?

Regards

Clive

I will assume that the table has an integer identity primary key. The SQL then becomes very simple:

SELECT A,B,C FROM TABLENAME WHERE ID = (SELECT MAX(Id) FROM TABLENAME)

|||

Yes the ID field is exactly as you say. That works perfectly - many thanks

Clive

|||

Another way to do is:

select top 1 visitid from medications order by visitid desc

It will return the last row only.

No comments:

Post a Comment