Click to See Complete Forum and Search --> : want to access only the last record of a database
Ashish Rajan
April 26th, 2005, 12:49 AM
hi everyone,
i m actually posting this thread again.
i want to access only the last record of a table.
there are no date and time fields in the table.
by checking the last record i want to know whether any new records have been added since the last access. so that i can display only the records which have been added recently.
all this through visual c++.
plz help.
thanx
Ashish
Andy Tacker
April 26th, 2005, 04:34 AM
If you have any column which is "Identity", you can always have the last record number you accessed. so, in next query you can create a condition using this "Identity".
basically you need to have some unique identificator. using this unique identificator, you can always sort your records.
klintan
April 26th, 2005, 04:34 AM
Assuming you have an id field that automatically increments, the following SQL will do the trick:
select top 1 <column list> from <table> order by <id> desc
example:
select top 1 * from Order order by OrderId desc
Ashish Rajan
April 26th, 2005, 06:14 AM
hi ppl,
thanx for helping me out.
isnt there anything similar to DatAddress.Recordset.MoveLast(vb) in vc++.
if i find some command similar to this in vc++ it would be great help.
thankx again
Ashish
klintan
April 26th, 2005, 06:45 AM
Well if you have the data loaded into a DataSet or DataTable, you can find the last row like this:
DataRow lastRow=null;
if (dt.Rows.Count>0) // dt is a datatable with the data
lastRow=dt.Rows[dt.Rows.Count-1];
rdrast
April 26th, 2005, 06:58 AM
Andy above is correct, you absolutely MUST have some record-unique, sequential value if you need to find the 'last' record in most normal databases.
Without one, you might indeed be retrieving the last physical record, but it may not be the last actual record...
Example:
Table-
Record 1
Record 2
Record 3
Now, you can say that indeed, record 3 is the last.. but if record 2 gets deleted, it's space isn't actually purged from the physical table...
Table -
Record 1
(deleted rec)
Record 3
Next, you add a new record (which is obviously the most recent)
Table -
Record 1
Record 4 // new record added in old slack space
Record 3
At this point, Last row != last record. You need to be able to order your table by an auto-incrementing identifier, or date/time to check last entry written.
codeguru.com
Copyright WebMediaBrands Inc., All Rights Reserved.