Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Wrong Identity returned from SCOPE_IDENTITY()

Author  Topic 

yakoo
Constraint Violating Yak Guru

312 Posts

Posted - 2002-05-31 : 15:58:28
I have an app running that executes a stored procedure that returns an Identity.

The problem. The stored procedure is called 15 times in a row. The first 12 calls return the appropriated identity from the table but the 13th call returns an identity that isnt even in the database (100706792).

1. call sp_AddItem - record is added with ID of 3201 and 3201 is returned
2. call sp_AddItem - record is added with ID of 3202 and 3202 is returned
3. call sp_AddItem - record is added with ID of 3203 and 3203 is returned
4. call sp_AddItem - record is added with ID of 3204 and 3204 is returned
5. call sp_AddItem - record is added with ID of 3205 and 3205 is returned
6. call sp_AddItem - record is added with ID of 3206 and 3206 is returned
7. call sp_AddItem - record is added with ID of 3207 and 3207 is returned
8. call sp_AddItem - record is added with ID of 3208 and 3208 is returned
9. call sp_AddItem - record is added with ID of 3209 and 3209 is returned
10. call sp_AddItem - record is added with ID of 3210 and 3210 is returned
11. call sp_AddItem - record is added with ID of 3211 and 3211 is returned
12. call sp_AddItem - record is added with ID of 3212 and 3212 is returned
13. call sp_AddItem - record is added with ID of 3213 and 100706792 is returned

The sp_AddItem simply executes an INSERT statment and calls SCOPE_IDENTITY after that statement. I have checked the tables and there is no replication that would cause the Identity to increase.

Any one have any ideas?

Got SQL?

setbasedisthetruepath
Used SQL Salesman

992 Posts

Posted - 2002-05-31 : 16:04:42
that's freakish. can you post both the dml for sp_Additem and the dml that calls it?

setBasedIsTheTruepath
<O>
Go to Top of Page

izaltsman
A custom title

1139 Posts

Posted - 2002-05-31 : 16:06:31
And while you are at it, let us know what does DBCC CHECKIDENT ('table_name', NORESEED) says for that table of yours...

Go to Top of Page

yakoo
Constraint Violating Yak Guru

312 Posts

Posted - 2002-05-31 : 16:10:22
TABLE

CREATE TABLE [dbo].[HelpItems] (
[ItemID] [int] IDENTITY (1, 1) NOT FOR REPLICATION NOT NULL ,
[ShortName] [varchar] (25) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[ParentID] [int] NULL ,
[menu_order] [int] NULL ,
[FeatureToken] [char] (15) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[hidden] [bit] NULL ,
[Name] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[Title] [varchar] (100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[XMLText] [text] COLLATE SQL_Latin1_General_CP1_CI_AS NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO

ALTER TABLE [dbo].[HelpItems] WITH NOCHECK ADD
CONSTRAINT [PK_HelpItems] PRIMARY KEY NONCLUSTERED
(
[ItemID]
) ON [PRIMARY]
GO

ALTER TABLE [dbo].[HelpItems] ADD
CONSTRAINT [FK_HelpItems_HelpItems] FOREIGN KEY
(
[ParentID]
) REFERENCES [dbo].[HelpItems] (
[ItemID]
)
GO



I have looked over this for an hour, even with another developer, and we do not notice anything wrong with the code.


CREATE Procedure sp_CreateNewHelpItem(
@ShortName varchar(255),
@ParentID integer,
@MenuOrder integer,
@FeatureToken char(15),
@Hidden bit,
@Name varchar(50),
@Title varchar(100),
@XMLText as text,
@ItemID integer OUTPUT
)
AS
IF @ParentID = 0
SELECT @ParentID = NULL


INSERT INTO HelpItems (ShortName, ParentID, Menu_Order, FeatureToken, Hidden, Name, Title, XMLText)VALUES (@ShortName, @ParentID, @MenuOrder, @FeatureToken, @Hidden, @Name, @Title, @XMLText)

SELECT @ItemID = SCOPE_IDENTITY()

RETURN 1
GO



code that calls it is VB COM

Dim DBConnection As Connection, cmd As Command

Set DBConnection = New Connection
DBConnection.Open DBConns.Item("DB").DBConn
Set cmd = New Command
cmd.CommandType = adCmdStoredProc
cmd.ActiveConnection = DBConnection
cmd.CommandText = "sp_CreateNewHelpItem"
cmd.Parameters.Append cmd.CreateParameter("RETURN_VALUE", adInteger, adParamReturnValue)
cmd.Parameters.Append cmd.CreateParameter("@ShortName", adVarChar, adParamInput, 25)
cmd.Parameters.Append cmd.CreateParameter("@ParentID", adInteger, adParamInput)
cmd.Parameters.Append cmd.CreateParameter("@MenuOrder", adInteger, adParamInput)
cmd.Parameters.Append cmd.CreateParameter("@FeatureToken", adChar, adParamInput, 15)
cmd.Parameters.Append cmd.CreateParameter("@Hidden", adBoolean, adParamInput)
cmd.Parameters.Append cmd.CreateParameter("@Name", adVarChar, adParamInput, 50)
cmd.Parameters.Append cmd.CreateParameter("@Title", adVarChar, adParamInput, 100)
cmd.Parameters.Append cmd.CreateParameter("@XMLText", adLongVarChar, adParamInput, 40000)
cmd.Parameters.Append cmd.CreateParameter("@ItemID", adInteger, adParamOutput)

cmd.Parameters("@ShortName") = ShortName
cmd.Parameters("@MenuOrder") = Order
cmd.Parameters("@FeatureToken") = FeatureToken
cmd.Parameters("@ParentID") = ParentID
cmd.Parameters("@Hidden") = Hidden
cmd.Parameters("@Name") = Name
cmd.Parameters("@Title") = Title
cmd.Parameters("@XMLText") = XMLText
cmd.Execute

If cmd.Parameters("RETURN_VALUE") = 1 Then
NextHelpItemID = cmd.Parameters("@ItemID").Value
//here is where the weird identity is returned
Else
NextHelpItemID = 0
End If

Set cmd = Nothing
DBConnection.Close
Set DBConnection = Nothing


Got SQL?
Go to Top of Page

yakoo
Constraint Violating Yak Guru

312 Posts

Posted - 2002-05-31 : 16:11:40
quote:

And while you are at it, let us know what does DBCC CHECKIDENT ('table_name', NORESEED) says for that table of yours...



Checking identity information: current identity value '3213', current column value '3213'.
DBCC execution completed. If DBCC printed error messages, contact your system administrator.

Got SQL?
Go to Top of Page

setbasedisthetruepath
Used SQL Salesman

992 Posts

Posted - 2002-05-31 : 16:23:15
You might want to check the ado errors collection after the if in:

If cmd.Parameters("RETURN_VALUE") = 1 Then
NextHelpItemID = cmd.Parameters("@ItemID").Value


in the procedure, you also might want to check the value of @@error immediately following the INSERT.

setBasedIsTheTruepath
<O>
Go to Top of Page

yakoo
Constraint Violating Yak Guru

312 Posts

Posted - 2002-05-31 : 16:25:47
Thanks, I will try that and get back to you

Got SQL?
Go to Top of Page

yakoo
Constraint Violating Yak Guru

312 Posts

Posted - 2002-05-31 : 16:38:16
I added another parameter to the Stored Procedure called @errCode

After I do the insert I run SELECT @errCode = @@ERROR

the value that I got back for this was 100707640 (which isnt found in BOL), and the identity was 100707144, even though the real identity was 3104. (I reimported some data).

There were no errors reported under the ADODB.Connection.Errors object.

Any other suggestions?

Got SQL?
Go to Top of Page

izaltsman
A custom title

1139 Posts

Posted - 2002-05-31 : 16:48:56
And does this stored procedure work OK when you run it from Query Analyzer?

Go to Top of Page

yakoo
Constraint Violating Yak Guru

312 Posts

Posted - 2002-05-31 : 16:49:12
I ran it again and got error code of 100706792 and identity of 100706416 when the actual identity in the DB is 3104.

This is getting very aggrevating.

Got SQL?
Go to Top of Page

yakoo
Constraint Violating Yak Guru

312 Posts

Posted - 2002-05-31 : 17:08:23
I ran it in QA and it worked perfectly fine. I even put a 25 counter while loop around it and all the values returned matched those that were inserted in the DB.

the values seem to be corrupting as they are being given back to COM via ADO

any suggestions on how to debug this within the sp to see that the value there, when called by COM, is being initially passed back correctly?

Got SQL?
Go to Top of Page

izaltsman
A custom title

1139 Posts

Posted - 2002-05-31 : 17:59:05
That is very odd... Maybe (and this is pure speculation on my part) there is some kind of problem with your MDAC. Try verifying the integrity of your MDAC installation with the component checker, and re-installing version 2.6 SP1 or 2.7
You can download MDAC Component Checker, and the latest MDAC versions here:
http://www.microsoft.com/data/



Go to Top of Page
   

- Advertisement -