Avatar of axnst2
axnst2
Flag for United States of America asked on

Increment an int value by one in a single update

Hi Experts,

    When I do an update on the table I need to automatically increment one of the fields something like so:

UPDATE MyTable
SET
        Value1 = 'NewValue'
        Value2 = Value2 + 1


Is there a way to do this somehow?

Value2 is used kind of like a version number!  Don't ask!

Thanks!
Microsoft SQL Server 2005Microsoft SQL Server

Avatar of undefined
Last Comment
axnst2

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Saurabh Bhadauria

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Member_2_861731

You can do just as you posted,

UPDATE MyTable
SET
        Value2 = Value2 + 1
Steve Wales

The problem you may be getting is your syntax, you're missing a comma, just before 'Value2 =':

UPDATE MyTable
SET
        Value1 = 'NewValue',
        Value2 = Value2 + 1

Open in new window

awking00

I assume you have a where clause that will filter only those rows you wish to update.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
axnst2

ASKER
Thank you!