February 28, 2006 at 4:05 am
The problem is to find the latest inserted last 3 records in the table
the table is not maintaining any timestamp or date field or identity field
February 28, 2006 at 4:14 am
I'm afraid, the table has no fields as well
Do you have a primary key that incorporates some logic, sort of identity-like procedure?
Otherwise, if you do not intend to add some field, i suggest, you create a small history table and a trigger which always records the last insert, while deleting the oldest insert in the history table. Don't forget to add an id to the history table
_/_/_/ paramind _/_/_/
February 28, 2006 at 4:25 am
SQL does not keep any form of rowid or insert time that you can use to pick up frst or last inserted record.
If you want to be able to get the last inserted records(s) then you need one of the following - identity column, timestamp, datetime defaulting to getdate
The history table is one option to get the last inserted, other is to add one of the above columns. Providing the app that uses the db is properly written (doesn't do things like insert into TableB select * from TableA) then the addition of another column shouldn't cause any problems.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
February 28, 2006 at 5:00 am
Fortunately (or unfortunately depending on your background and point of view), SQL Server doesn't attempt to give any sort of row pointers like a file-system based database (eg FoxPro) would. This frees MS to make changes to the underlying structure knowing that no one could or would be relying on it for their app to function properly. It should also encourage developers to think in set-based terms rather than cursor/procedural terms. As others have said, you'll need some sort of extra column to provide the ordering...
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply