Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 155
  • Last Modified:

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.
0
johnson1
Asked:
johnson1
  • 2
1 Solution
 
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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now