Solved

Increment an int value by one in a single update

Posted on 2013-01-08
5
212 Views
Last Modified: 2013-01-08
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!
0
Comment
Question by:axnst2
5 Comments
 
LVL 12

Accepted Solution

by:
Saurabh Bhadauria earned 500 total points
ID: 38755076
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
 
LVL 13

Expert Comment

by:LIONKING
ID: 38755079
You can do just as you posted,

UPDATE MyTable
SET
        Value2 = Value2 + 1
0
 
LVL 22

Expert Comment

by:Steve Wales
ID: 38755090
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
 
LVL 32

Expert Comment

by:awking00
ID: 38756406
I assume you have a where clause that will filter only those rows you wish to update.
0
 

Author Closing Comment

by:axnst2
ID: 38757130
Thank you!
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question