Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 87563
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T22:25:21+00:00 2026-05-10T22:25:21+00:00

I know I can update a single record like this – but then how

  • 0

I know I can update a single record like this – but then how to I get access to the id of the record that was updated? (I’m using MSSQL so I can’t use Oracles RowId)

update myTable set myCol = 'foo' where itemId in (select top 1 itemId from myTable ) 

If I was peforming an Insert I could use getGeneratedKeys to get the id field value, but I don’t think there is an equivalent for an update?

I know I can use a scrollable resultset to do what I want

i.e.

stmt = conn.prepareStatement('select top 1 myCol, itemId from myTable', ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); ResultSet resultSet = stmt.executeQuery(); if(resultSet.first()){     resultSet.updateString(1, 'foo');     resultSet.updateRow();     String theItemId = resultSet.getString(1) } resultSet.close(); 

but I’m concerned about performance as testing shows lock timeouts under load and I was wondering if there was a better/simpler way?

— EDIT: Just to finalise this issue… When we migrate to MSSQL2005 we will upgrade our code to use Rich’s answer. In the current release we have used the lock hints: (UPDLOCK ROWLOCK READPAST) to mitigate the performance problems our original code showed.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T22:25:21+00:00Added an answer on May 10, 2026 at 10:25 pm

    This example works really well in MSSQL 2005…

    SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO  DROP TABLE [dbo].[TEST_TABLE] GO  CREATE TABLE [dbo].[TEST_TABLE](     [id] [int] IDENTITY(1,1) NOT NULL,     [name] [nvarchar](100) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,  CONSTRAINT [PK_TEST_TABLE] PRIMARY KEY CLUSTERED  (     [id] ASC )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY] ) ON [PRIMARY]   -- An insert which will return the identity INSERT INTO [dbo].[TEST_TABLE] ([name])  OUTPUT inserted.id VALUES('Test 1')  -- Another insert which will return the identity INSERT INTO [dbo].[TEST_TABLE] ([name])  OUTPUT inserted.id VALUES('Test 2')  -- Now an update which will return the identity UPDATE [dbo].[TEST_TABLE] SET [name] = 'Updated Test 1' OUTPUT inserted.id WHERE [name] = 'Test 1'  SELECT id, [name] FROM [dbo].[TEST_TABLE] 

    And more specifically to your query…

    update myTable set myCol = 'foo' output inserted.itemid where itemId in (select top 1 itemId from myTable ) 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 140k
  • Answers 140k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer There are a few points to make here: Firstly, you… May 12, 2026 at 7:49 am
  • Editorial Team
    Editorial Team added an answer For some reason when i remove the style on my… May 12, 2026 at 7:49 am
  • Editorial Team
    Editorial Team added an answer The problem is not which Queue implementation you use but… May 12, 2026 at 7:49 am

Related Questions

I know I can update a single record like this - but then how
I am wondering if there is a way to make ASP.NET controls play nicely
I've been thinking about batch reads and writes in a RESTful environment, and I
I've got a script that takes a user uploaded RTF document and merges in
When I entered my current (employer's) company a new database schema was designed and

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.