Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8171381
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T21:26:38+00:00 2026-06-06T21:26:38+00:00

I need to get the last inserted row in a specific table for strictly

  • 0

I need to get the last inserted row in a specific table for strictly the current session. I can’t use @@IDENTITY and SCOPE_IDENTITY() as they will return the last inserted identity for ANY table. The problem with IDENT_CURRENT is that it will return the last inserted identity of a record for a specific table but for ANY session. This is a problem for me because the INSERT is called by multiple sessions, and I specifically need the identity of the last inserted record for my specific session.

Any pointers how to accomplish this?

ps. the INSERT statement is not inside a SPROC, so SPROC solutions are not viable.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T21:26:40+00:00Added an answer on June 6, 2026 at 9:26 pm

    I know it’s not the answer you seem to want to hear, but the answer is to use SCOPE_IDENTITY(). The problem you are thinking of (where it will be for any table) is why we use SCOPE_IDENTITY() instead of @@IDENTITY. Consider the case where you have a table with an IDENTITY column, and an insert trigger on that table that itself inserts into a table with an IDENTITY column.

    CREATE TABLE dbo.Log(LogID INT IDENTITY(100,1), FooID INT);
    
    CREATE TABLE dbo.Foo(FooID INT IDENTITY(1,1), name VARCHAR(32));
    GO
    
    CREATE TRIGGER dbo.Foo_Insert
    ON dbo.Foo
    FOR INSERT
    AS
    BEGIN
      SET NOCOUNT ON;
      INSERT dbo.Log(FooID) SELECT FooID FROM inserted;
    END
    GO
    

    Now, your situation is that you want a reliable way to retrieve the ID after an insert. SCOPE_IDENTITY() gives you that, since it is restricted to your scope, while @@IDENTITY is not restricted to your scope (meaning it will grab the last IDENTITY issued, which happened in the triggers’ scope, not your scope:

    INSERT dbo.Foo(name) SELECT 'Bob';
    
    SELECT
      @@IDENTITY,
      SCOPE_IDENTITY();
    

    Results:

    ----  ----
    100   1
    

    Note that neither SCOPE_IDENTITY() nor @@IDENTITY should be used in the case where you insert multiple rows. The way to do this would be to use the OUTPUT clause. First let’s drop the trigger:

    DROP TRIGGER dbo.Foo_Insert;
    

    Now let’s test a multi-row insert:

      INSERT dbo.Foo(name) 
        OUTPUT inserted.FooID, inserted.name
        SELECT 'Frank' UNION ALL SELECT 'Jim';
    

    Results:

    FooID  name
    -----  -----
    2      Frank
    3      Jim
    

    If you have conditional inserts, there is no difference. Keeping the tables we have, let’s try this code twice:

    DECLARE @table SYSNAME;
    SET @table = N'Log';
    
    IF @table = N'Log'
    BEGIN
        INSERT dbo.Log(FooID) SELECT 10;
    END
    
    IF @table = N'Foo'
    BEGIN
        INSERT dbo.Foo(name) SELECT 'Tom';
    END
    
    SELECT SCOPE_IDENTITY();
    

    Result:

    ----
    101
    

    Let’s try it again with N'Foo':

    DECLARE @table SYSNAME;
    SET @table = N'Foo';
    
    IF @table = N'Log'
    BEGIN
    INSERT dbo.Log(FooID) SELECT 10;
    END
    
    IF @table = N'Foo'
    BEGIN
        INSERT dbo.Foo(name) SELECT 'Tom';
    END
    
    SELECT SCOPE_IDENTITY();
    

    Results:

    ----
    4
    

    If it’s more complex than that (e.g. you may insert into more than one table), you can do something like:

    IF <some conditional>
    BEGIN
        INSERT dbo.sometable ...
        SET @somevar = SCOPE_IDENTITY();
    END
    IF <some other conditional>
    BEGIN
        INSERT dbo.some_other_table ...
        SET @some_other_var = SCOPE_IDENTITY();
    END
    

    I’m not sure why you think this doesn’t work, and I’m not sure why I have to go to this length to convince you that it does. Again, if you show an example where this doesn’t work (or what “ANY table” you think might interfere), we might be able to comment. As it stands it sounds like your opinion of SCOPE_IDENTITY() is based on something you’ve heard about @@IDENTITY. These assumptions are quite easy to prove or disprove yourself.

    As an aside, IDENT_CURRENT should not even be mentioned in this conversation. It is not safe to use for concurrent activity at all, and you should pretend that you’ve never heard of it as far as I’m concerned. You should also consider the same for @@IDENTITY – I can’t think of a valid use for it unless you really do want to capture, from outside of the trigger, the IDENTITY generated inside a trigger.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to get the last inserted id of table that have multi-column primary
How can I fetch the last row that was inserted using DBI ( DBD::mysql
I need to get the last inserted product by a user. This is what
I need to get the records which are inserted in last 24 hours. I
I need to get the last part of current directory, for example from /Users/smcho/filegen_from_directory/AIRPassthrough
Possible Duplicate: PHP: how to get last inserted ID of a table? This is
I need to select the last row inserted into a database immediately after writing
Possible Duplicate: How to get last inserted id? I have a table Absences |
I need to get the last string content of the url between / and
I need to get the last day of the month given as a date

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.