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.
Author |
Topic |
cybersurfer
Starting Member
4 Posts |
Posted - 2007-01-02 : 04:27:22
|
Hi allHow 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? |
 |
|
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. |
 |
|
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) |
 |
|
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 MyTableORDER BY MySortColumn DESCKristen |
 |
|
cybersurfer
Starting Member
4 Posts |
Posted - 2007-01-02 : 05:26:57
|
Thanks |
 |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
|
|
|