Solved

Increment an int value by one in a single update

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

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

744 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

13 Experts available now in Live!

Get 1:1 Help Now