views:

228

answers:

2

I need to write a query that increments a value in a table by 3 when run.

I would like to do something like this but this doesn't work.

UPDATE table SET value = (SELECT value FROM table WHERE condition = true) + 3 WHERE condition = true

As in the title this is a DB2 database, any ideas?

EDIT: Actually this does work, could also do the + 3 in the select. I just had some stuff in the wrong place with the casting I had to do Thanks in advance

+1  A: 

I think what you are looking for is simply

UPDATE table SET value = value + 3 WHERE condition = TRUE

Does that work?

If you want all rows where condition = true to have (for example) 3+ the max value of any row for which the condition is true, use this:

UPDATE table SET value = 
(
    SELECT MAX(value) 
    FROM table WHERE condition = true
) + 3 
WHERE condition = true
Roee Adler
A: 

Sorry, the confusion was probably that the query above would actually work for what I wanted to do. The condition I have will always return only 1 row. See the edit above for more details

Corey
Please delete this, it's a comment rather than an answer
Roee Adler