我现在有一个表 A 
区域CD  商品CD   店CD   key
1      AS       3      NULL
1      AS       4      NULL
1      AS       5      NULL 
2      8765     1      NULL
2      8765     5      NULL
3      09       98     NULL
3      09       12     NULL
3      09       13     nULL根据 区域CD 和 商品CD 为组 对 key列 赋值  
key值 是从 B表 取出来的 B表 结构 一列 一个整数 假如现在是  0123 
 当区域CD 1  和商品CD As 赋值是 0123 +1 位 0124 
区域CD 2 和商品CD 8765  赋值是  0125 
区域CD 3 和 商品CD 09  赋值是 0126 我现在 用 游标实现上面的赋值了   速度 有点慢   不知道 还有没有 别的 实现方法??
求大虾 解答??     

解决方案 »

  1.   

    数据量有那么大慢是无法避免的
    建议采用while循环避免使用游标,游标占用资源太多
    while循环里进行update top(10000) ...where ...再waitfor delay的方式
    虽然持续时间长,但不会占用服务器太多资源影响其他进程了。
      

  2.   

    用存储过程,将key传递过来
    update tb set [key]=( case when 区域CD=2 and 商品CD='8765' then right('0000'+(cast(@key as int)+区域CD),4) when …… then …… else null end)
      

  3.   

    A 表数据Key列的数据和  B表的数据和结构,和想要的效果
      

  4.   

    A 表的数据是 动态 变化的  
    B表 是一个 字段 Int行 初始值 是1  最大 9999
    想要的 结果就是 根据区域CD和商品CD分组 为 A表 key 赋值 
      

  5.   

    区域CD  商品CD   店CD   key
     1      AS       3      0001
     1      AS       4      0001
     1      AS       5      0001 
     2      8765     1      0002
     2      8765     5      0002
     3      09       98     0003
     3      09       12     0003
     3      09       13     0003
     
      

  6.   

    create table tb(区域CD int, 商品CD varchar(100),店CD int,[key] varchar(100))
    go
    insert into tb
    select 1,'AS',3,NULL union all
    select 1,'AS',4,NULL union all
    select 1,'AS',5,NULL  union all
    select 2,'8765',1,NULL union all
    select 2,'8765',5,NULL union all
    select 3,'09',98,NULL union all
    select 3,'09',12,NULL union all
    select 3,'09',13,NULL
    go
    update t set [key]=right('0000'+cast(row as varchar(10)),4) from(select row=dense_rank()over(order by 区域CD,商品CD),* from tb )t
    go
    select * from tb
      

  7.   

    update tb set [key]=right('0000'+cast(区域CD as varchar(10)),4) where 1=1