BUG: @@IDENTITY in Nested Stored Procedure Returns NULL

Last reviewed: May 2, 1997
Article ID: Q139620

The information in this article applies to:
  • Microsoft SQL Server, version 6.0
BUG# NT: 11897 (6.00)

SYMPTOMS

The @@identity global variable will be NULL under the following conditions:

  1. If it is queried in a stored procedure after a nested stored procedure call and insert.

  2. The nested stored procedure performs an insert and queries the @@identity global variable as well.

WORKAROUND

Do not query the @@identity global variable from more than one stored procedure nesting level.

For example, modify the "add_test1" stored procedure (from example in the following MORE INFORMATION section) to the following code and it works properly:

   CREATE PROCEDURE add_test1 AS
   EXECUTE add_test2
   EXECUTE add_test2
   RETURN
   GO

STATUS

Microsoft has confirmed this to be a problem in Microsoft SQL Server version 6.0. We are researching this problem and will post new information here in the Microsoft Knowledge Base as it becomes available.

MORE INFORMATION

The following script shows the @@identity global variable returning a NULL value:

   CREATE TABLE test1
   (
   id int IDENTITY(1,1) NOT NULL ,
   name char (20) NOT NULL
   )
   GO
   CREATE PROCEDURE add_test2 AS
   INSERT test1 VALUES ('1')
   SELECT @@identity
   RETURN
   GO
   CREATE PROCEDURE add_test1 AS
   EXECUTE add_test2
   INSERT test1 VALUES ('2')
   SELECT @@identity
   RETURN
   GO
   EXECUTE add_test1
   GO


Additional query words: sql6 windows nt sproc sp
Keywords : kbbug6.00 kbprg SrvProg SSrvStProc
Version : 6.0
Platform : WINDOWS


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: May 2, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.