To get latest 3 inserted records from a table without date field or time stamp

  • 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

     

  • 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 _/_/_/

  • 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

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • 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