What will this new column be used for? This type of requirement is usually not a good design.but...Here is one way you can do itNOTE: that with this method the clustered index and an anchor variable is necessary to insure the order the updates take placeCREATE TABLE #valueset (k1 int identity, c1 int)set nocount onINSERT #valueset (c1) VALUES (7)INSERT #valueset (c1) VALUES (0)INSERT #valueset (c1) VALUES (3)INSERT #valueset (c1) VALUES (4)INSERT #valueset (c1) VALUES (7)INSERT #valueset (c1) VALUES (0)INSERT #valueset (c1) VALUES (7)INSERT #valueset (c1) VALUES (1)INSERT #valueset (c1) VALUES (5)goalter table #valueset add primary key clustered (k1)alter table #valueSet add newCol int nullgodeclare @nc int, @dummyAnchor intset @nc = 1update #valueSet set @nc = newCol = case when c1 = 7 then 1 else @nc + 1 end ,@dummyAnchor = k1 --according to Jeff Moden, besides the clustered index, this is also necessary to guarantee the order of the updatesselect * from #valueset godrop table #valuesetoutput:k1 c1 newCol----------- ----------- -----------1 7 12 0 23 3 34 4 45 7 16 0 27 7 18 1 29 5 3
Be One with the OptimizerTG