如何从DbVisualizer中删除表中的重复行

时间:2013-01-30 07:09:50

标签: sql dbvisualizer

我有一个名为ORG_MAP的表,它包含200条记录,我执行了一个查询,将这200条记录复制到另一个名为ORG_MAP_1的表中(ORG_MAP的重复表),但是发生了相同的200条记录被插入到同一ORG_MAP表中。

现在我要删除那些重复的记录。我不知道该怎么做。

请帮帮我。我正在使用db visualizer8.0.11版本。

我使用的查询是:

INSERT INTO METRICS.ORG_MAP (REPORT_END_DATE,ENTITY,ENTITY_TYPE,RELATIONSHIP,TARGET,TARGET_TYPE)
SELECT REPORT_END_DATE,ENTITY,ENTITY_TYPE,RELATIONSHIP,TARGET,TARGET_TYPE
FROM METRICS.ORG_MAP

1 个答案:

答案 0 :(得分:0)

我不认为你的问题有任何直接的解决方案 但我做了一些工作可能对你有用....

create table #table(id int,c varchar(2))
insert into #table values('1','a')
insert into #table values('1','a')
insert into #table values('2','b')
insert into #table values('2','b')
insert into #table values('3','c')
insert into #table values('3','c')
insert into #table values('4','d')
insert into #table values('5','d')

declare @table table(uid int,id int ,c varchar(2));


with tbl as (
    select ROW_NUMBER() OVER(ORDER BY id DESC) AS uid ,id,c from #table
) 

insert into @table select * from tbl

declare @tmpid int,@tmpuid int,@cnt int
declare tmpc cursor 
for select uid,id from @table;

open tmpc

fetch next from tmpc 
into @tmpuid,@tmpid

while @@FETCH_STATUS = 0
BEGIN
set @cnt = (select COUNT(id) from @table where id = @tmpid)
IF (@cnt > 1)
BEGIN   
    delete from @table where uid = @tmpuid      
END

fetch next from tmpc 
into @tmpuid,@tmpid

END 

CLOSE tmpc; 
DEALLOCATE tmpc;

select * from @table

drop table #table