加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

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

发布时间:2020-12-12 15:29:31 所属栏目:MsSql教程 来源:网络整理
导读:?以下是本人在建立多维数据集时对重复客户信息处理所采用的存储过程: create?procedure Sp_DeleteDuplicateCustomerInfo as ?declare @CustomerId bigint ?declare @Count int ?declare @TopCount int ?declare @StrCustomerId char(7) ?declare @StrTopCoun

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读