FIX: @@IDENTITY Returns NULL If Trigger Inserts into Table Without IDENTITY Column

Article translations Article translations
Article ID: 199688 - View products that this article applies to.
This article was previously published under Q199688
BUG #: 53856 (SQLBUG_70)
Expand all | Collapse all

SYMPTOMS

In SQL Server 7.0, a SELECT @@IDENTITY statement returns NULL after an insert into a table with a trigger, and that trigger inserts into another table that does NOT have an IDENTITY column. In SQL Server 6.5, the SELECT @@IDENTITY statement returns the value inserted into the IDENTITY column of the base table. The SQL Server 7.0 Books Online incorrectly states that the SQL Server 7.0 behavior is correct.

WORKAROUND

To retrieve the identity value inserted into the base table, please see the following article in the Microsoft Knowledge Base:
163446 PRB: Guarantee @@IDENTITY Value on a Per Table Basis

STATUS

Microsoft has confirmed this to be a problem in SQL Server 7.0. This problem has been corrected in U.S. Service Pack 1 for Microsoft SQL Server 7.0. For more information, click the following article number to view the article in the Microsoft Knowledge Base:
232570INF: How to Obtain Service Pack 1 for Microsoft SQL Server 7.0 and Microsoft Data Engine (MSDE) 1.0
For more information, contact your primary support provider.

MORE INFORMATION

The @@IDENTITY variable is a per-user variable that contains the value of the IDENTITY field from the last insert performed by the user. The following table summarizes the @@IDENTITY value for SQL Server versions 6.5 and 7.0:

Collapse this tableExpand this table
ActionSQL Server 6.5 @@IDENTITY valueSQL Server 7.0 @@IDENTITY value
INSERT to table A
No IDENTITY Col,
No Trigger
NULLNULL
INSERT to table B
w/ IDENTITY Col,
No Trigger
bb
INSERT to table B
w/ IDENTITY Col,
Trigger inserts to
table C, w/ IDENTITY col
cc
INSERT to table B
w/ IDENTITY Col,
Trigger inserts to
table D, No IDENTITY col
bNULL


In the table above, "b" is the value inserted into the IDENTITY column for table B, and "c" is the value inserted into the IDENTITY column for table C.

The SQL Server 6.5 behavior is useful in cases where an INSERT trigger inserts rows into an audit table that does not have an IDENTITY column. This allows the user to retrieve the identity value inserted into the base table by simply using a SELECT @@IDENTITY statement. In SQL Server 7.0, this incorrectly returns NULL.

Properties

Article ID: 199688 - Last Review: March 14, 2006 - Revision: 2.1
APPLIES TO
  • Microsoft SQL Server 7.0 Standard Edition
Keywords: 
kbbug kbfix KB199688

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com