Solved

insert get identity - does not return correct number

Posted on 2014-09-03
3
141 Views
Last Modified: 2014-09-03
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
Comment
Question by:johnson1
  • 2
3 Comments
 
LVL 69

Accepted Solution

by:
ScottPletcher earned 500 total points
ID: 40301582
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
 
LVL 69

Expert Comment

by:ScottPletcher
ID: 40301586
DECLARE @identity TABLE (
    id int
    )

INSERT INTO MyTable(MyNum, MyText) OUTPUT INSERTED.$IDENTITY INTO @identity values(3,'Mm')
0
 

Author Closing Comment

by:johnson1
ID: 40301752
Thank you very much. There was a trigger.
0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Join & Write a Comment

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now