Solved

Increment an int value by one in a single update

Posted on 2013-01-08
5
214 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

Suggested Solutions

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
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.

730 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