Ramesh Srinivas
asked on
Returning the last inserted ID...
Hi all,
I have in the past been able to retrieve the ID of the last inserted record - but by creating a separate command to retrieve it after the insert command. What i want to do is to have one command that inserts data and returns the ID.
I have tried the following.....
I used this SQL to insert and retrieve the ID:
CREATE PROCEDURE SP_StreamJob
@DOB DateTime,
@EndDate DateTime = Null,
@CompanyID int,
@EmailAddr nvarchar(500),
@Feature nvarchar(250) = Null,
@Prog nvarchar(250) = Null,
@Channel nvarchar(150) = Null,
@TOB nvarchar(5) = Null,
@FileName nvarchar(1000),
@FileType nvarchar(10),
@CreatedBy nvarchar(150),
@theID int = null OUTPUT
AS
BEGIN
SET NOCOUNT ON
Insert Into dbo.TBL_StreamClips
(dbo.TBL_StreamClips.DOB,
dbo.TBL_StreamClips.End_Da te,
dbo.TBL_StreamClips.CPY_ID ,
dbo.TBL_StreamClips.Email_ Addr,
dbo.TBL_StreamClips.Featur e_Artist,
dbo.TBL_StreamClips.Progra mme,
dbo.TBL_StreamClips.Channe l,
dbo.TBL_StreamClips.TOB,
dbo.TBL_StreamClips.Date_A dded,
dbo.TBL_StreamClips.Create d_By,
dbo.TBL_StreamClips.FileNa me,
dbo.TBL_StreamClips.FileTy pe)
Values
(@DOB,
@EndDate,
@CompanyID,
@EmailAddr,
@Feature,
@Prog,
@Channel,
@TOB,
GETDATE(),
@CreatedBy,
@FileName,
@FileType)
Select @theID = SCOPE_IDENTITY()
SET NOCOUNT OFF
END
GO
Then with my command i do the following:
Dim theID = cmdInsert.ExecuteScalar()
The problem is that it keeps returning 0 (Zero).
When I run in query analyser and print out @theID I get the correct ID.
Probably something really small i am missing, any ideas???
thanks,
KS
I have in the past been able to retrieve the ID of the last inserted record - but by creating a separate command to retrieve it after the insert command. What i want to do is to have one command that inserts data and returns the ID.
I have tried the following.....
I used this SQL to insert and retrieve the ID:
CREATE PROCEDURE SP_StreamJob
@DOB DateTime,
@EndDate DateTime = Null,
@CompanyID int,
@EmailAddr nvarchar(500),
@Feature nvarchar(250) = Null,
@Prog nvarchar(250) = Null,
@Channel nvarchar(150) = Null,
@TOB nvarchar(5) = Null,
@FileName nvarchar(1000),
@FileType nvarchar(10),
@CreatedBy nvarchar(150),
@theID int = null OUTPUT
AS
BEGIN
SET NOCOUNT ON
Insert Into dbo.TBL_StreamClips
(dbo.TBL_StreamClips.DOB,
dbo.TBL_StreamClips.End_Da
dbo.TBL_StreamClips.CPY_ID
dbo.TBL_StreamClips.Email_
dbo.TBL_StreamClips.Featur
dbo.TBL_StreamClips.Progra
dbo.TBL_StreamClips.Channe
dbo.TBL_StreamClips.TOB,
dbo.TBL_StreamClips.Date_A
dbo.TBL_StreamClips.Create
dbo.TBL_StreamClips.FileNa
dbo.TBL_StreamClips.FileTy
Values
(@DOB,
@EndDate,
@CompanyID,
@EmailAddr,
@Feature,
@Prog,
@Channel,
@TOB,
GETDATE(),
@CreatedBy,
@FileName,
@FileType)
Select @theID = SCOPE_IDENTITY()
SET NOCOUNT OFF
END
GO
Then with my command i do the following:
Dim theID = cmdInsert.ExecuteScalar()
The problem is that it keeps returning 0 (Zero).
When I run in query analyser and print out @theID I get the correct ID.
Probably something really small i am missing, any ideas???
thanks,
KS
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hmmm, both methods did not work.
The first method error was:
System.IndexOutOfRangeExce ption: An SqlParameter with ParameterName '@theID' is not contained by this SqlParameterCollection.
The second method error was:
System.NullReferenceExcept ion: Object reference not set to an instance of an object.
The first method error was:
System.IndexOutOfRangeExce
The second method error was:
System.NullReferenceExcept
ASKER
I got it!
I added the output parameter like so:
Dim idParam As SqlParameter = cmdInsert.Parameters.Add(" @theID", SqlDbType.Int)
idParam.Direction = ParameterDirection.Output
and then executed the nonquery:
cmdInsert.ExecuteNonQuery( )
finally, retreived the output parameters value like so:
Dim mID As Integer = idParam.Value
Thanks for your help,
KS
I added the output parameter like so:
Dim idParam As SqlParameter = cmdInsert.Parameters.Add("
idParam.Direction = ParameterDirection.Output
and then executed the nonquery:
cmdInsert.ExecuteNonQuery(
finally, retreived the output parameters value like so:
Dim mID As Integer = idParam.Value
Thanks for your help,
KS
that;s good =)
==========
cmdInsert.ExecuteNonQuery(
Dim id As Integer = DirectCast(cmd.Parameters(
or
change sql from
Select @theID = SCOPE_IDENTITY()
to
SELECT SCOPE_IDENTITY()
and this would work
Dim id As Integer = DirectCast(cmd.ExecuteScal