Solved

Increment an int value by one in a single update

Posted on 2013-01-08
5
211 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

932 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now