微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

SQLServer中用存储过程去除表中重复客户信息

 以下是本人在建立多维数据集时对重复客户信息处理所采用的存储过程:

create procedure Sp_DeleteDuplicateCustomerInfo
as
 declare @CustomerId bigint
 declare @Count int
 declare @TopCount int
 declare @StrCustomerId char(7)
 declare @StrTopCount char(1)
 
 declare deleteCur cursor for
 select CustomerId,count(customerID) counts from T_CustomerInfo group by customerID
having count(customerID)>1
 open deleteCur
 fetch next from deleteCur into @CustomerId,@Count
 while @@fetch_status=0
 begin
  set @TopCount=@Count-1
  set @StrCustomerId = cast(@CustomerId as char(7))
  set @StrTopCount = cast(@TopCount as char(1))
  print 'delete from T_CustomerInfo where recId in
  (select top '+cast(@TopCount as char(1))+' recId from T_CustomerInfo where CustomerId='+cast(@CustomerId as char(7))

  exec('delete from T_CustomerInfo where recId in
  (select top '+@StrTopCount+' recId from T_CustomerInfo where CustomerId='+@StrCustomerId+')')

  fetch next from deleteCur into @CustomerId,@Count
 end
 close deleteCur
 deallocate deleteCur

go

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐