How to get Last inserted row in SQL 2014

durai raj s 20 Reputation points
2023-03-24T06:56:58.65+00:00

In my SQL Table having more than 100000 Rows , Every 1 Min the new row is inserted into the Table.How to get Last inserted row

I planned for Trigger Query based on the Last inserted row

SQL Server Other
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. LiHongMSFT-4306 31,566 Reputation points
    2023-03-24T07:27:03.8266667+00:00

    Hi @durai raj s

    If there is an identity column in this table, then you could use IDENT_CURRENT('tableName') which returns the last identity value generated for a specific table in any session and any scope.

    Best regards,

    Cosmog Hong


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. Erland Sommarskog 121.4K Reputation points MVP Volunteer Moderator
    2023-03-25T12:53:22.07+00:00

    You need some column in the table that can identify the row as the most recent. If you don't have that, you can't identify the row. A table is by definition an unordered set.


  3. Sreeju Nair 12,661 Reputation points
    2023-03-26T10:27:06.09+00:00

    In addition to the above responses, i.e. in addition to maintain and identity column, another option to add a field of type DateTime that stores the inserted date and time. You could create the column to the table, and add a default value for that column as getdate()

    Now, when you insert a new row, automatically the inserted time will be recorded in the field. Now you can use the select clause with order by to retrieve the data in the order of the date field you just created.

    Hope this helps

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.