David Thielen
David Thielen

Reputation: 32874

Can I increment values in a SQL record using an update?

I want to do something like the following (in Sql Server):

update person set numItems += @num, today = @updateDate where id = @id

I know the "numItems += @num" is not correct syntax, how do I write that part?

Upvotes: 0

Views: 55

Answers (2)

Ruslan
Ruslan

Reputation: 2797

SQL Server doesn't have a += notation, so just expand it fully

update person set numItems = numItems + @num, today = @updateDate where id = @id

Upvotes: 2

Gordon Linoff
Gordon Linoff

Reputation: 1269513

Some databases do support +=. However, the following is standard SQL:

update person
    set numItems = numItems + @num,
        today = @updateDate
where id = @id;

Upvotes: 1

Related Questions