insert get identity - does not return correct number

Hello,
I have a table with identity column (Id).  I.e.   MyTable(Id int, MyNum int, MyText varchar(50), CreatedBy int)
I insert into it like this   MyTable(MyNum, MyText)values(3,'Mm')
Then I want to get the identity of the newest column.  I do it like this:
SELECT @@Identity.
The newest number in the identity column is 20993, but I get returned 1005.
I do not understand why this is. Can somebody please help.
johnson1Asked:
Who is Participating?
 
Scott PletcherSenior DBACommented:
There could be a trigger on the table.  @@identity is the last one for any table in any scope.

Use function SCOPE_IDENTITY() instead; or use the OUTPUT clause on the INSERT to get the identity value.
0
 
Scott PletcherSenior DBACommented:
DECLARE @identity TABLE (
    id int
    )

INSERT INTO MyTable(MyNum, MyText) OUTPUT INSERTED.$IDENTITY INTO @identity values(3,'Mm')
0
 
johnson1Author Commented:
Thank you very much. There was a trigger.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.