14

@@IDENTITY returns the ID of the last row inserted, I want to retrieve the ID of the last row updated.

Here is my query:

UPDATE [Table] 
SET Active = 1, 
    Subscribed = 1, 
    RenewDate = GETDATE(),
    EndDate = DATEADD(mm,1,getdate()),
WHERE SC = @SC
  AND Service = @Ser

How do I get the ID of this updated row?

The column is called TableID and I'm not using it in the query.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
HelpASisterOut
  • 3,085
  • 16
  • 45
  • 89

4 Answers4

17

You cannot retrieve an ID since there is no ID being inserted.....

But you can:

  1. just query the table using the same criteria as in your UPDATE:

    SELECT TableID 
    FROM dbo.Table
    WHERE SC = @SC AND Service = @Ser  -- just use the same criteria
    
  2. use the OUTPUT clause on the UPDATE to get that info:

    UPDATE [Table] 
    SET Active = 1, 
        Subscribed = 1, 
        RenewDate = GETDATE(),
        EndDate = DATEADD(mm,1,getdate())
    OUTPUT Inserted.TableId       -- output the TableID from the table
    WHERE SC = @SC AND Service = @Ser
    

Read more about the OUTPUT clause on Technet - it can be used on INSERT and DELETE as well

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Great post & it helped me out a lot. It is odd that the Microsoft T-SQL "property" is named _Inserted_ though because an update actually runs. My point here is that I created a Stored Proc & I need the value of the updated value & then later (under maintenance) someone will see that i'm doing OUTPUT Inserted.[FieldName] & it's not readily apparent that this is actually the value which was updated. – raddevus Feb 07 '22 at 20:28
  • 1
    @raddevus: well - one *could* argue that an `UPDATE` really could be a `delete` on the old data (therefore, that's available in `Deleted`), and then an `insert` of the new data (thus that being available in `Inserted`). It's not done that way - but might be a mental model to understand what those pseudo tables are called .... – marc_s Feb 07 '22 at 20:43
  • That is very interesting. Thanks for making that point. You made me think more deeply about what is really going on. – raddevus Feb 07 '22 at 20:46
7

you can try using this:

OUTPUT INSERTED.TableID 

in your code it would look like this:

    UPDATE [Table] 
    SET Active = 1, 
        Subscribed = 1, 
        RenewDate = GETDATE(),
        EndDate = DATEADD(mm,1,getdate())
OUTPUT INSERTED.TableID 
    WHERE SC = @SC
      AND Service = @Ser

Hope this helps.

raddevus
  • 8,142
  • 7
  • 66
  • 87
Fizor
  • 1,480
  • 1
  • 16
  • 31
4

I guess you need this one,

UPDATE [Table] 
SET Active = 1, 
    Subscribed = 1, 
    RenewDate = GETDATE(),
    EndDate = DATEADD(mm,1,getdate())
    OUTPUT INSERTED.TABLE_PrimaryKeyID
WHERE SC = @SC
AND Service = @Ser

Main source: here

Community
  • 1
  • 1
BAdmin
  • 927
  • 1
  • 11
  • 19
-1

Try using select @@identity gives last updated identity for the particular session (or) select scope_identity gives last updated identity for the particular scope (or) select ident_curr('tablename') give the last updated identity regardless of the session or scope but for that particular table.

sneha
  • 1