Re: updating a numeric field-accumulating value -
12-29-2011
, 04:33 PM
If I understand what your post correctly, this solution should work for you:
WITH CTE AS (
SELECT v_pid, dense_rank() OVER (ORDER BY MAPBLOLOT) AS newvalue
FROM p_real
)
UPDATE CTE
SET v_pid = newvalue
There is no need for a cursor here.
In the query above, I first define a CTE (Common Table Expression) where I
return the column to update, and the numbering you want. You may want to run
that query first (with the MAPBLOLOT column added) to see that the results
are what you expected.
Then I update the CTE. This looks like little funny, but SQL Server finds
the way back to the base table and performs the update.
The roundabout with the CTE is needed, because of the dense_rank() function;
it would not be possible to use it directly in the SET clause. |