sql在没有主键的情况下删除表中的重复字段

1.查询不重复的数据存到临时表里,删除掉原表,然后将临时表的数据存到原表里,上代码:
[sql] 
  1. Select distinct * into Tmp from a  
  2. Drop table a  
  3. Select * into a from Tmp  
  4. Drop table Tmp  
Select distinct * into Tmp from a
Drop table a
Select * into a from Tmp
Drop table Tmp
2.当原表和其他表有关联时,删除整个表可能造成数据乱掉,因此可以在表中新增一列自增的临时列,删除数据后再将这一列删除,上代码: 
[sql] 
  1. alter table a add newfield int identity(1,1);  
  2. delete a 
  3.     where newfield not in 
  4.        (select min(newfield) from a group by Prodid,Proddes)  
  5. alter table a drop column newfield  
alter table a add newfield int identity(1,1);
delete a
where newfield not in
(select min(newfield) from a group by Prodid,Proddes)
alter table a drop column newfield




版权声明:本文为qq_34386891原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。