I can’t figure out how to get the SCOPE_IDENTITY() back to my variables from an SQL2005 Store Procedure.
My sSQL String:
sSQL = 'EXEC [sp_NewClaim] ' & Chr(34) & ClaimNumber & Chr(34) & ', ' & Request.Cookies('UserID') & ', ' & Request.Cookies('MasterID') & ', ' & Chr(34) & strRestaurante & Chr(34) & ', ' & Chr(34) & Fecha & Chr(34) & ', ' & Chr(34) & Hora & Chr(34) & ', ' & Chr(34) & Request('Tiempo') & Chr(34) & ', ' & Chr(34) & Request('Luz') & Chr(34) & ', ' & Chr(34) & Request('Desc') & Chr(34) & ', ' & Chr(34) & Request('incidente') & Chr(34) & ', ' & Chr(34) & Request('codigos') & Chr(34) & ', False, 0; SELECT RecordNumber = SCOPE_IDENTITY()'
My sSQL Output:
EXEC [sp_NewClaim] 'W200811', 7, 8, 'Otro -- WORK PLEASE', '11/19/2008', '01:19 PM', 'Nublado', 'Mala', 'asdasd', 'uyiuyui', 'C-Junta', False, 0; SELECT RecordNumber = SCOPE_IDENTITY()
Executing my SQL Command:
Set rsData= Server.CreateObject('ADODB.Recordset') rsData.Open sSQL, conDB, adOpenKeyset, adLockOptimistic
Trying to Output the SCOPE_IDENTITY() Produces an Empty Variable (No Output):
Response.Write('<br />Record Number: ' & rsData('RecordNumber'))
The Store Procedure runs correctly. My Information gets stored into my database with out problems. RecordNumber is the Column with the Identity, and the Store Procedure has defined @RecordNumber as an Output:
USE [db_clcinsurance_com] GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE PROCEDURE sp_NewClaim ( @ClaimNumber nvarchar(50), @blah............ ................. @RecordNumber INT OUTPUT ) AS BEGIN INSERT INTO Accidente (ClaimNumber,........., RecordNumber) VALUES (@ClaimNumber,....., @RecordNumber) SET @RecordNumber = SCOPE_IDENTITY(); END
For your stored procedure, do this:
And then get the id the same way you’d retrieve any other query result.