Article ID: 319699 - View products that this article applies to.
This article was previously published under Q319699
This article has been archived. It is offered "as is" and will no longer be updated.
BUG #: 101772 (SQLBUG_70)
When you insert a value into a table by using the INSERT EXEC syntax in a stored procedure, @@IDENTITY does not reflect the new value that you are inserting; however, if you use EXEC with an explicit INSERT INTO TABLENAME VALUES () statement, @@IDENTITY reflects the new identity value.
The following example demonstrates that if you use INSERT...EXEC to insert into a table, the value that is returned for @@IDENTITY is NULL even though the statement adds a row to the table with an identity of 1:
The preceding code sample returns NULL as if no value were inserted.
The preceding code sample reflects the correct @@IDENTITY value.
To resolve this problem, obtain the latest service pack for Microsoft SQL Server 7.0. For additional information, click the following article number to view the article in the Microsoft Knowledge Base:
(http://support.microsoft.com/kb/301511/EN-US/ )INF: How to Obtain the Latest SQL Server 7.0 Service Pack
To work around this problem, use the INSERT ...VALUES syntax to insert values if you must use @@IDENTITY to get the last identity value. Otherwise, use SELECT MAX(IDENTITYCOL) FROM TABLENAME to get the correct value for the last inserted identity value if this is what you want.
Microsoft has confirmed that this is a problem in the Microsoft products that are listed at the beginning of this article. This problem was first corrected in Microsoft SQL Server 7.0 Service Pack 4.