Latest inserted records in tbl

  • Hi,

    I have tried the select top 1000 rows and found out that it does not show the latest inserted/ added rows in any table. Do I have to fetch rows via latest date? please suggest any sample code to do this. Thanks!

  • unless you have some way to order them in insertion order, you can't. You'd need a column that's either an incrementing identity column or a datetime field with a default of the insertion date.

  • so, I looked at the table again and it has Date column, so will it work if I query based on the date column? if you have any sample query please share.

  • So, you are asking us a question when we have no knowledge of the table or the data that is being stored in the table. So, a shot in the dark: it depends on what data is stored in that date column.

     

    • This reply was modified 4 years, 8 months ago by  Lynn Pettis.
  • DateCreated? DateModified? DateOfBirth? DateOfNextScheduledDentistVisit? ....

    If it contains the date the row was created, you can do something like this

    SELECT TOP (1000) 
    col1, col2, ...
    FROM tbl
    ORDER BY <DateColumn> DESC

    The absence of evidence is not evidence of absence.
    Martin Rees

    You can lead a horse to water, but a pencil must be lead.
    Stan Laurel

  • You should read and understand the documentation on any functionality before you try to use it.

    https://docs.microsoft.com/en-us/sql/t-sql/queries/top-transact-sql?view=sql-server-ver15

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Thanks, this did the trick, It was date inserted or created column

     

    SELECT TOP (1000)

    col1, col2, ...

    FROM tbl

    ORDER BY <DateColumn> DESC

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply