要从表结构设计上重新审视你的设计思路。C列既然是计算列,就不要出现在永久表里。需要查询某行或某些行的记录时,可以临时计算这些记录的C值。这样,也不用对整个表都计算C值了。

解决方案 »

  1.   

    同意楼上的,感觉像在PMP。
    有时在解决某个问题时如果值不值得,得换一个思路了。
    嘻嘻~~~这是竹之草说的
      

  2.   

    create table t1 (a int identity(1,1),b int)
    insert t1(b)
    select 30
    union all select 50
    union all select 60
    union all select 20
    union all select 40select * into #aa from(
    select *,d=(select sum(b) from t1 bb where bb.a<=aa.a) from t1 aa
    )cc
    update #aa
    set d=case
      when a<(select  top 1 a from #aa where d>=100)
      then b
      when a=(select  top 1 a from #aa where d>=100)
      then 100-(select d from #aa where a=(select top 1 a from #aa where d<100 order by d desc))
      else 0
      endselect * from #aa
    drop table #aa
    drop table t1
      

  3.   

    --测试
    create table t1 (a int identity(1,1),b int)
    insert t1(b)
    select 30
    union all select 50
    union all select 60
    union all select 20
    union all select 40select * into #aa from(
    select *,d=(select sum(b) from t1 bb where bb.a<=aa.a) from t1 aa
    )cc
    update #aa
    set d=case
      when a<(select  top 1 a from #aa where d>=100)
      then b
      when a=(select  top 1 a from #aa where d>=100)
      then 100-(select d from #aa where a=(select top 1 a from #aa where d<100 order by d desc))
      else 0
      endselect * from #aa
    --drop table #aa
    --drop table t1a         b        d
    --------------------   
    1 30 30
    2 50 50
    3 60 20
    4 20 0
    5 40 0