Intermittently this stored procedure will return an ID that is in the millions when it should actually only be around 400.
set ANSI_NULLS OFF
set QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[TestStoredProcedure]
(
@Title VARCHAR(50),
@ID INT OUTPUT
)
AS
DECLARE @ResultsTable Table(InsertedID INT);
INSERT INTO Table
(
Title
)
OUTPUT INSERTED.ID INTO @ResultsTable
VALUES
(
@Title
);
SELECT @ID = (SELECT TOP 1 InsertedID FROM @ResultsTable);
This stored procedure used to return the ID by using SCOPE_IDENTITY() but that had the same issue.
There are no triggers in the database. There are only the indexes on the primary keys that SQL Server creates automatically. There are no tables at all that have an ID anywhere near as large as what is being returned so I have no idea where these large numbers are coming from.
Any help or suggestions would be appreciated.
Edit: As I said above, this stored procedure originally used SCOPE_IDENTITY() like so:
set ANSI_NULLS OFF
set QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[TestStoredProcedure]
(
@Title VARCHAR(50),
@ID INT OUTPUT
)
AS
INSERT INTO Table
(
Title
)
VALUES
(
@Title
);
SELECT @ID = SCOPE_IDENTITY();
Edit 2:
The exact SQL version is:
Microsoft SQL Server 2005 - 9.00.4230.00 (X64) Jul 30 2009 13:42:21 Copyright (c) 1988-2005 Microsoft Corporation Standard Edition (64-bit) on Windows NT 5.2 (Build 3790: Service Pack 2)
Edit 3:
This is how the stored procedure is being called (good old classic ASP, it's a legacy site)
set obj_CMD = Server.CreateObject("ADODB.Command")
with obj_CMD
.ActiveConnection = Application("DSN")
.CommandText = "TestStoredProcedure"
.CommandType = adCmdStoredProc
dim txt_title
txt_title = "Some text"
set oParam = .CreateParameter("@Title",adVarChar,adParamInput,50,txt_title)
.Parameters.Append oParam
set oParam = .CreateParameter("@ID",adInteger,adParamOutput)
.Parameters.Append oParam
.Execute
dim ID
ID = .Parameters("@ID").Value
end with
set obj_CMD = nothing
Edit 4:
Running DBCC CHECKIDENT ('Table') returns:
Checking identity information: current identity value '422', current column value '422'. DBCC execution completed. If DBCC printed error messages, contact your system administrator.
This is as expected.