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

  • Home
  • SEARCH
  • 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 125603
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T05:11:06+00:00 2026-05-11T05:11:06+00:00

Imagine the following sql query: UPDATE MYTABLE SET COL2 = (SELECT COL2 + 1

  • 0

Imagine the following sql query:

UPDATE MYTABLE SET COL2 = (SELECT COL2 + 1 FROM (SELECT MAX(COL2) FROM MYTABLE) AS X) WHERE ID IN (1,2,3,4,5) 

Assume that before the update is executed MAX(COL2) is 1.

My intention is that for the update where ID=1 COL2 is updated to ‘max(COL2) + 1’ (i.e. 2), and that for subsequent updates ‘MAX(COL2) + 1’ is re-evaluated, so that for ID=2, COL2=3 and ID=3, COL2=4 etc…

What actually happens is that for all rows (ID=1,2,3,4,5), the value of COL2 is 2.

Is there a smart way to have the value of MAX(COL2) +1 ‘re-evaluated’ at each update? I realize there may be performance issues with doing this, but I am curious none-the-less! Is there a better alternative (that does not involve multiple update statements) ?

BTW: incase you are wondering about the syntax used for the above query (the nested inner table) see here: SQL : Using the target table in an UPDATE statement in a nested FROM clause

  • 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-11T05:11:07+00:00Added an answer on May 11, 2026 at 5:11 am
    UPDATE mytable, (   SELECT @loop := MAX(col1)   FROM     mytable   ) o SET col1 = (@loop := @loop + 1) 

    What you encountered here is called query stability.

    No query can see the changes made by itself, or the following query:

    UPDATE mytable SET col1 = col2 + 1 WHERE col1 > col2  

    would never end.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 114k
  • Answers 114k
  • 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 Mixing is the average of the two channels. f_mono =… May 11, 2026 at 10:15 pm
  • Editorial Team
    Editorial Team added an answer The following snippet will read whitespace-delimited numbers from a text… May 11, 2026 at 10:15 pm
  • Editorial Team
    Editorial Team added an answer With Visual C++, you can use __declspec(noreturn). May 11, 2026 at 10:15 pm

Related Questions

Using SQL Server 2005, I have a table where certain events are being logged,
Say I have the following data Name Value =============== Small 10 Medium 100 Large
I'm having trouble trying to optimize the following query for sql server 2005. Does
Sorry about the vague subject but I couldn't think what to put. Here's my

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.