如是一张表中姓名重复(有二个姓名相同)的有100多组,我想把二个相同姓名中的其中一个后面加字符"1",另外一个名字不变,Sql语句如何写

解决方案 »

  1.   

    ----------------------------------------------------------------
    -- Author  :fredrickhu(我是小F,向高手学习)
    -- Date    :2009-11-18 12:45:24
    -- Version:
    --      Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86) 
    -- Nov 24 2008 13:01:59 
    -- Copyright (c) 1988-2005 Microsoft Corporation
    -- Developer Edition on Windows NT 5.2 (Build 3790: Service Pack 1)
    --
    ----------------------------------------------------------------
    --> 测试数据:[tb]
    if object_id('[tb]') is not null drop table [tb]
    go 
    create table [tb]([name] varchar(10))
    insert [tb]
    select 'a' union all
    select 'a' union all
    select 'b' union all
    select 'b' union all
    select 'c' union all
    select 'c' union all
    select 'd'
    --------------开始查询--------------------------
    ;with f as
    (
    select id=row_number()over(partition by name order by name),* from [tb]
    )
    update f set name=name+'1' where id=2
    select * from tb
    ----------------结果----------------------------
    /*name
    ----------
    a
    a1
    b
    b1
    c
    c1
    d(7 行受影响) 
    */
      

  2.   

    rank_dense()...??????!!!!!!
      

  3.   


    if object_id('[tb]') is not null drop table [tb]
    go 
    create table [tb]([name] varchar(10))
    insert [tb]
    select 'a' union all
    select 'a' union all
    select 'a' union all
    select 'a' union all
    select 'a' union all
    select 'b' union all
    select 'b' union all
    select 'b' union all
    select 'b' union all
    select 'b' union all
    select 'c' union all
    select 'c' union all
    select 'c' union all
    select 'c' union all
    select 'c' union all
    select 'd' union all
    select 'd' union all
    select 'd' union all
    select 'd' union all
    select 'd'
    GOselect 
    name+
    case 
    when (ROW_NUMBER()over(partition by name order by name)-1=0) 
    then ''
    else cast((ROW_NUMBER()over(partition by name order by name)-1) as varchar) 
    end  
    from tb(20 行受影响)----------------------------------------
    a
    a1
    a2
    a3
    a4
    b
    b1
    b2
    b3
    b4
    c
    c1
    c2
    c3
    c4
    d
    d1
    d2
    d3
    d4(20 行受影响)
      

  4.   


    if object_id('[tb]') is not null drop table [tb]
    go 
    create table [tb]([name] varchar(10))
    insert [tb]
    select 'a' union all
    select 'a' union all
    select 'a' union all
    select 'a' union all
    select 'a' union all
    select 'a' union all
    select 'b' union all
    select 'b' union all
    select 'c' union all
    select 'c' union all
    select 'd'
    --------------开始查询--------------------------
    ;with f as
    (
    select id=row_number()over(partition by name order by name),* from [tb]
    )
    update f set name=name+CAST(ID-1 AS NVARCHAR(10)) WHERE ID>1
    select * from tb我无耻的抄袭