----创建一张临时表
CREATE TABLE #LiveCrystal_UserInfo_dy(
[Idx] [int] NULL,
[Id] [nvarchar](20) NULL,
[Insertdate] [datetime] NULL,
[Mac] [nvarchar](50) NULL,
[Phone_type] [nvarchar](50) NULL,
[Lmarkid] [nvarchar](10) NULL,
[Talkcanal] [int] NULL,
[Mtype] [int] NULL
)
---往临时表插入数据
insert into #LiveCrystal_UserInfo_dy
select Idx,Id,Insertdate,Mac,Phone_type,Lmarkid,Talkcanal,Mtype
from ( select Idx,Id,Insertdate,Mac,Phone_type,Lmarkid,Talkcanal,Mtype , ROW_NUMBER() OVER(PARTITION BY Id ORDER BY Insertdate desc )as ss
from TuiGather_log.dbo.LiveCrystal_UserInfo_2020) a where a.ss>1
declare @idx int
declare @id nvarchar(20)
declare @Insertdate datetime
declare @Mac nvarchar(50)
declare @Phone_type nvarchar(50)
declare @Lmarkid nvarchar(10)
declare @Talkcanal int
declare @Mtype int
---申明游标
declare id_cursor cursor
for(select * from #LiveCrystal_UserInfo_dy )
--打开游标
open id_cursor
--开始循环游标变量
fetch next from id_cursor into @idx, @id,@Insertdate,@Mac,@Phone_type,@Lmarkid,@Talkcanal,@Mtype
while(@@FETCH_STATUS=0) --返回被 FETCH语句执行的最后游标的状态
Begin
delete from TuiGather_log.dbo.LiveCrystal_UserInfo_2020 where idx=@idx and id=@id and Insertdate=@Insertdate and Mac=@Mac and Phone_type=@Phone_type and Talkcanal=@Talkcanal and Mtype=@Mtype
fetch next from id_cursor into @idx, @id,@Insertdate,@Mac,@Phone_type,@Lmarkid,@Talkcanal,@Mtype --转到下一个游标,没有会死循环
End
close id_cursor --关闭游标
deallocate id_cursor --释放游标
由于我这张临时表有 3077507的数据,这个操作结束需要5到6小时,效率极低,所以建议小数据量时使用