views:

167

answers:

6

Hi,

how I can do this: a=a+1 in sql server query?

Thanks!

+7  A: 

If you want to increase the value of a column in a specific row, this will do it for you:

UPDATE tablename
SET columnname = columnname + 1
WHERE primarykey = id

Other than that, please specify what you mean by your question.

Lasse V. Karlsen
+2  A: 

UPDATE mytable SET a = a + 1 WHERE row_condition

sebasgo
+1  A: 
UPDATE TableName SET ColumnName = ColumnName + 1 WHERE WhateverYouWant = WhatEverYouNeed
Ahmet Kakıcı
+7  A: 

You really need to provide more information... folk have given answers for updating a column; so for completeness here's how to declare, init and increment a variable:

DECLARE @a INT

SET @a = 10
SET @a = @a + 1
Chris J
Nice, I was about to answer the same.
Adrian Godong
+1  A: 

It's also legal to update variables in SQL 2005 using the following

declare @a int

select column1, column2, @a = @a + column3
from table
where condition

It's similar to Ahmet KAKICI's answer just using a local variable.

Mike J
+1  A: 

SQL Server 2008 introduced new T-SQL syntax for "Compound Assignment Operators"

DECLARE @price AS MONEY = 10.00;
SET @price += 2.00;
SELECT @price;
John Sansom