IDENT_CURRENT equivalent for current session

后端 未结 1 1876
说谎
说谎 2021-01-15 23:26

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 wil

相关标签:
1条回答
  • 2021-01-16 00:02

    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 讨论(0)
提交回复
热议问题