How to get value of a primary key as soon as you inserted it (2/2)

The previous example works with JDBC 3.0. However, my server doesn’t use the 3.0 implementation, so I had to do it by using a stored procedure which returns the key for the inserted row (SCOPE_IDENTITY() value).

Here’s the skeleton for how I did it:

CREATE PROCEDURE YOUR_STORED_PROCEDURE_NAME (
 [YOUR_PARAMETERS_GO_HERE],
 @RowId INT OUTPUT
)AS

INSERT INTO YOUR_TABLE [ETC ….] ;

SET @RowId = SCOPE_IDENTITY();
select @RowId;
GO

Advertisements
This entry was published on January 22, 2009 at 1:53 pm and is filed under Database. Bookmark the permalink. Follow any comments here with the RSS feed for this post.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: