Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Last Entry in Database Table

Author  Topic 

cybersurfer
Starting Member

4 Posts

Posted - 2007-01-02 : 04:27:22
Hi all

How do retrieve the last entry from a database table using the 'SELECT' statement?

Sorry, I'm new to certain aspects of the SQL programming language.

Thanks

miranwar
Posting Yak Master

125 Posts

Posted - 2007-01-02 : 04:54:20
Does the table have a Clustered Index based on an identity Column or alternatively a datetime field for insert date?
Go to Top of Page

cybersurfer
Starting Member

4 Posts

Posted - 2007-01-02 : 05:00:23
The table does have a datetime field that is used when a new transaction is logged.

I simply want the last row that is in the table.
Go to Top of Page

miranwar
Posting Yak Master

125 Posts

Posted - 2007-01-02 : 05:12:56
Something like this this should bring back the last row inserted into the table:

select * from mytable where mydatetimefield = (select max(mydatetimefield) from mytable)
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2007-01-02 : 05:21:04
"last entry"

There is no concept of "last entry" in a relational database, so you need to define that by something that you can order the records. Create Date (although that may NOT be unique!) or an incrementing value column (such as IDENTITY as miranwar suggested).

Then you can do:

SELECT TOP 1 Col1, Col2, ...
FROM MyTable
ORDER BY MySortColumn DESC

Kristen
Go to Top of Page

cybersurfer
Starting Member

4 Posts

Posted - 2007-01-02 : 05:26:57
Thanks
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-01-02 : 07:04:48
Learn SQL
http://www.sql-tutorial.net/
http://www.firstsql.com/tutor.htm
http://www.w3schools.com/sql/default.asp


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -