【发布时间】:2019-06-30 08:24:46
【问题描述】:
declare
cursor cur1 is select * from address where aid in
(select Min(aid) from address group by
country,state,city,street_name,locality,house_no);
cursor cur2 is select * from address;
cur1_aid address.aid%type;
cur1_country address.country%type;
cur1_city address.city%type;
cur1_state address.state%type;
cur1_streetAddress address.street_name%type;
cur1_locality address.locality%type;
cur1_houseNo address.house_no%type;
cur2_aid address.aid%type;
cur2_country address.country%type;
cur2_city address.city%type;
cur2_state address.state%type;
cur2_streetAddress address.street_name%type;
cur2_locality address.locality%type;
cur2_houseNo address.house_no%type;
begin
open cur1;
loop
fetch cur1 into cur1_aid,cur1_country,cur1_state,cur1_city,cur1_streetAddress,cur1_locality,cur1_houseNo;
exit when cur1%NOTFOUND;
open cur2;
loop
fetch cur2 into cur2_aid,cur2_country,cur2_state,cur2_city,cur2_streetAddress,cur2_locality,cur2_houseNo;
exit when cur2%NOTFOUND;
if(cur1_country=cur2_country) and (cur1_state=cur2_state) and (cur1_city=cur2_city) and (cur1_streetAddress=cur2_streetAddress) and (cur1_locality=cur2_locality) and (cur1_houseNo=cur2_houseNo) then
if (cur1_aid!=cur2_aid) then
update employee_add set aid=cur1_aid where aid=cur2_aid;
delete address where aid=cur2_aid;
end if;
end if;
end loop;
close cur2;
end loop;
close cur1;
DELETE FROM employee_add a
WHERE ROWID > (SELECT MIN(ROWID) FROM employee_add b
WHERE b.eid=a.eid and b.aid=a.aid
);
end;
/
我有三个表 Employee(eid,ename) ,Address(aid,country,state,city,streetaddress,locality,houseNo) 和一个关系表 (M2M) MANY TO MANY TABLE employee_add(eid,aid),
我想从地址表和employee_add 表中删除重复项而不丢失数据
【问题讨论】:
-
所以这是基于the answer to your previous question on the subject。 (是的,删除您的用户帐户并没有删除您的问题。)该解决方案有什么问题?请定义您所说的“有效解决方案”是什么意思。
-
实际上有人告诉我这会起作用,但这段代码效率不高,不知道如何改进......
标签: oracle plsql duplicates