Connecting Tech Pros Worldwide Forums | Help | Site Map

retrieve a last value from sql

Member
 
Join Date: Dec 2008
Location: Pakistan
Posts: 56
#1: Jul 29 '09
Hi,
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

Regards,
Syed Ahmed Hussain

Familiar Sight
 
Join Date: Sep 2008
Posts: 253
#2: Jul 29 '09

re: retrieve a last value from sql


Could try the below, not sure if it works in SQL though.
Expand|Select|Wrap|Line Numbers
  1. SELECT MAX(column) FROM tablename
Newbie
 
Join Date: Jun 2008
Posts: 25
#3: Aug 4 '09

re: retrieve a last value from sql


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.
Member
 
Join Date: Dec 2008
Location: Pakistan
Posts: 56
#4: Aug 5 '09

re: retrieve a last value from sql


Both of you are right....

Thank you guys.. :)

Regards,

Syed Ahmed Hussain
OuTCasT's Avatar
Needs Regular Fix
 
Join Date: Jan 2008
Location: South Africa
Posts: 355
#5: Aug 7 '09

re: retrieve a last value from sql


If you have an identity column then u could use

Expand|Select|Wrap|Line Numbers
  1. select @@Identity from myTable
Member
 
Join Date: Dec 2008
Location: Pakistan
Posts: 56
#6: Aug 7 '09

re: retrieve a last value from sql


So what will this query do?
Quote:
select @@Identity from myTable
OuTCasT's Avatar
Needs Regular Fix
 
Join Date: Jan 2008
Location: South Africa
Posts: 355
#7: Aug 11 '09

re: retrieve a last value from sql


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)
Reply