有表A  
字段 xm,bh,  sfz  
现因sfz字段有重复值  
如  
xm                    bh                            sfz  
张三    01       431026  
李四    04       431026  
 
如何将李四的身份证(sfz)加1成为431027  
 
xm                    bh                            sfz  
张三    01       431026  
李四    04       431027  
 
请教高手  

解决方案 »

  1.   

    create table A(xm varchar(20), bh varchar(20), sfz varchar(20))
    insert A select  '张三', '01', '431026'
    union all select '李四', '04', '431026'
    select id=identity(int, 1, 1), xm, bh,sfz into #T from Aselect * from #Tselect xm, bh, 
    sfz=(case when exists(select 1 from #T where id<tmp.id and sfz=tmp.sfz) then 
       (select max(sfz)+1 from #T)
      else tmp.sfz end)
    from #T as tmp--result
    xm                   bh                   sfz         
    -------------------- -------------------- ----------- 
    张三                   01                   431026
    李四                   04                   431027(2 row(s) affected)
      

  2.   

    这样写有问题,当数据变成为:
    create table tb(xm nvarchar(50), bh nvarchar(50), sfz int)
    insert tb select '张三', '01', 431026
    union all select '李四', '04', 431026
    union all select '李A', '04', 431027
    union all select '李b', '04', 431027李四+1后就和李A重复了。
      

  3.   

    update a set sfz=431027 where xm='李四'