Click here to Skip to main content
15,918,108 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I need some help in updating a database table.
I have got a table with int values and i want to update it
so as each value will be its current value + some number i choose.
the question is how i get the current number efficiently?
thanks in advance :)
Posted

DECLARE @ProductID INT
DECLARE @HitCount INT
DECLARE @UPDATEHIT CURSOR
SET @UPDATEHIT = CURSOR FOR
 SELECT ProductID FROM Product
OPEN @UPDATEHIT 
FETCH NEXT
 FROM @UPDATEHIT INTO @ProductID
WHILE @@FETCH_STATUS = 0
 BEGIN
 UPDATE Production.Product
  SET HitCount =(Select HitCount+1 From Product Where ProductID=@ProductID)
 Where ProductID=@ProductID 

FETCH NEXT
FROM @UPDATEHIT INTO @ProductID
 END
CLOSE @UPDATEHIT 
DEALLOCATE @UPDATEHIT 
 
Share this answer
 
Comments
Espen Harlinn 30-May-11 19:25pm    
A "little" overkill - but it's an aswer - so have my 5
Monjurul Habib 31-May-11 4:03am    
thank you. I thought the OP was looking for recursive update, anyway I actually misunderstood the question.
Hw about something like this:
update table1 set value=value+number where id=1


http://www.w3schools.com/sql/sql_update.asp[^]

http://msdn.microsoft.com/en-us/library/ms177523.aspx[^]

Best regards
Espen Harlinn
 
Share this answer
 
Comments
Monjurul Habib 31-May-11 4:04am    
nice answer, my 5.
Espen Harlinn 31-May-11 5:25am    
Thank you, Monjurul!
thx, solved by update table1 set value+='num' where id=1
 
Share this answer
 
Comments
Espen Harlinn 30-May-11 19:28pm    
Thanks, it's custumary to use the "Add Comment" link on the asnwer - and if you're happy with the answer, mark it as the answer. Anyway, happy coding :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900