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!
axnst2Asked:
Who is Participating?
 
Saurabh BhadauriaConnect With a Mentor Commented:
you can write a update query as you mentioned

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


or another way can be to write a trigger...
0
 
LIONKINGCommented:
You can do just as you posted,

UPDATE MyTable
SET
        Value2 = Value2 + 1
0
 
Steve WalesSenior Database AdministratorCommented:
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

0
 
awking00Commented:
I assume you have a where clause that will filter only those rows you wish to update.
0
 
axnst2Author Commented:
Thank you!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.