表test
Id Int Identity(1,1)  cy (varchar(50))   dy  (varchar(50))
--------------------  ---------------    -------------------
1                     0755               null
2                     0755               null
3                     0755               null
4                     0769               null
5                     0769               null
6                     0753               null现要填补dy字段的值
要求dy字段的值等于cy字段值,但是如果dy字段不能有重复值
如果有重复即在后面加编号1,2,3......

Id Int Identity(1,1)  cy (varchar(50))   dy  (varchar(50))
--------------------  ---------------    -------------------
1                     0755               0755
2                     0755               07551
3                     0755               07552
4                     0769               0769
5                     0769               07691
6                     0753               0753

解决方案 »

  1.   

    update a from tb a
        set dy = cy +
               isnull(rtrim(nullif((select count(*) from tb where cy=a.cy and id<a.id),0)),'')
      

  2.   

    好像有问题哦消息 156,级别 15,状态 1,第 1 行
    关键字 'from' 附近有语法错误。
    消息 102,级别 15,状态 1,第 3 行
    ',' 附近有语法错误。
      

  3.   


    update test set dy=cy+
    isnull(nullif((select count(1) from test where cy=a.cy and id<a.id)+1,0),'') from test a
      

  4.   

    create table #(
    id int identity(1,1),
    cy varchar(50),
    dy varchar(50)
    )
    insert #(cy) select '0755'
    insert #(cy) select '0755'
    insert #(cy) select '0755'
    insert #(cy) select '0769'
    insert #(cy) select '0769'
    insert #(cy) select '0753'declare @a int
    update a set @a=(select count(1) from # where cy=a.cy and id<a.id),
                 dy=cy+case when @a>0 then cast(@a as varchar) else '' end
    from # aselect * from #drop table #/*
    id          cy                                                 dy                                                 
    ----------- -------------------------------------------------- -------------------------------------------------- 
    1           0755                                               0755
    2           0755                                               07551
    3           0755                                               07552
    4           0769                                               0769
    5           0769                                               07691
    6           0753                                               0753
    (所影响的行数为 6 行)
    */
      

  5.   

    liangCK 是对的
    谢谢各位参与,尤其是fcuandy 这么热心。
      

  6.   

    ]
    不好意思,刚才随手敲的。没经测试。敲错了
    create table dsd(
    id int identity(1,1),
    cy varchar(50),
    dy varchar(50)
    )
    insert dsd(cy) select '0755'
    insert dsd(cy) select '0755'
    insert dsd(cy) select '0755'
    insert dsd(cy) select '0769'
    insert dsd(cy) select '0769'
    insert dsd(cy) select '0753'
    update dsd set dy=cy+isnull(nullif((select cast(count(1) as varchar) from dsd where cy=a.cy and id<a.id),0),'') from dsd a