Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Updating rows with incremented values

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2002-05-22 : 10:23:54
Ash writes "I have a table with fields that act as counters. What is the fastest way to perform an increment on these fields? The only way I can think of, is to perform a SELECT to get the original values, and then an UPDATE with the original value+1. I don't like this way because it's 2 db accesses. I have to do this increment on maybe 30 fields at a time, and that means 60 db accesses (yuck). What can I do? Please help me."

JustinBigelow
SQL Gigolo

1157 Posts

Posted - 2002-05-22 : 10:35:56
What you want should require only two executions, not sixty.

simplest way...

update foo
set field1 = field1 + 1
set field2 = field2 + 1
...
set field30 = field30 + 1


It maybe a pain in the ass to type but if you put it in a stored procedure you would only have to do it once. It also sounds like your table structure could possibly be improved to make a function like this easier. Care to share your table design?

Justin




Edited by - justinbigelow on 05/22/2002 10:36:39
Go to Top of Page
   

- Advertisement -