我现在一个急需大家解决的问题.我们系统有几个大类的编号是八位,全部建错啦,现在我用了这样的语句:
update cpda 
set cpbh=left(cpbh,8)
where cpbh like '306%01'
提示:
服务器: 消息 2627,级别 14,状态 1,行 1
违反了 PRIMARY KEY 约束 'PK_cpda'。不能在对象 'cpda' 中插入重复键。
语句已终止。
我准备把CPDA把改成非主键,但怕会对我数据出现问题.请哪位高手帮帮忙!本人先行谢过!

解决方案 »

  1.   

    楼主可以将left(cpbh,8)之后重名的行都找出来,然后修改这些行以保证left(cpbh,8)之后不相同.查找这些行的代码类似如下:
    declare @t table(id int identity,name varchar(10))
    insert @t(name)
    select '3060000101' union all
    select '3060000202' union all
    select '30600004' union all
    select '30600003' union all
    select '30600002' union all
    select '30600001'select * from @t a where 
    exists(select 1 from @t where left(name,8) = left(a.name,8) group by left(name,8) having count(*) > 1)/*结果
    id   name
    -------------------------------------
    1    3060000101
    2    3060000202
    5    30600002
    6    30600001
    */
      

  2.   


    违反了 PRIMARY KEY 约束 'PK_cpda'。不能在对象 'cpda' 中插入重复键。将PK_cpda约束删除后,再试着用你自己的语句看看