By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,843 Members | 2,311 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,843 IT Pros & Developers. It's quick & easy.

retrieve a last value from sql

P: 79
Can any one tell me .. How to retrieve a last value from the database?
I tried the last function, but I just got the error.

Thanks in advance

Syed Ahmed Hussain
Jul 29 '09 #1
Share this Question
Share on Google+
6 Replies

P: 384
Could try the below, not sure if it works in SQL though.
Expand|Select|Wrap|Line Numbers
  1. SELECT MAX(column) FROM tablename
Jul 29 '09 #2

Expert 100+
P: 145
Correct me if i'm wrong, but do you mean last row entered into a table?
Expand|Select|Wrap|Line Numbers
  1. SELECT MAX(MySequentialIndexColumn) from [MyTable]
works based on the premise that MySequentialIndexColumn is an IDENTITY column continuously incremented with each addition to the table. Therefore, the highest value of the column should be the latest entry.

Here's my advice:

If it is important to know when a row was added to a table the time/date of insertion should be listed as a field. With the addition of a time/date field, you will be able to query your table based on the exact time rows were entered.
Aug 4 '09 #3

P: 79
Both of you are right....

Thank you guys.. :)


Syed Ahmed Hussain
Aug 5 '09 #4

P: 374
If you have an identity column then u could use

Expand|Select|Wrap|Line Numbers
  1. select @@Identity from myTable
Aug 7 '09 #5

P: 79
So what will this query do?
select @@Identity from myTable
Aug 7 '09 #6

P: 374
if u have and identity column which set ur rows in order 1 2 3 4 5 etc
then it will pull the last value inserted in the database.

i add a index column like so

Expand|Select|Wrap|Line Numbers
  1. alter table MYTable
  2. Add TempID int IDENTITY(1,1)
Aug 11 '09 #7

Post your reply

Sign in to post your reply or Sign up for a free account.