Oracle 删除重复记录

DELETE FROM TABLE WHERE ROWID NOT IN
(SELECT MAX(rowID) FROM TABLE GROUP BY NEWCODE)
解释:
      1:SELECT MAX(rowID) FROM TABLE GROUP BY NEWCODE,按照新的字段(newcode)分组;

      2:然后查找分组后的rowId,其中rowId可以为MIN(rowId)或MAX(rowId);
     
      3:DELETE FROM TABLE WHERE ROWID NOT IN (SELECT MAX(rowID) FROM TABLE GROUP BY NEWCODE) 进行删除重复记录
      4:该语句执行效果:当newCode中有一列属性有重复值时,就会删除;
     5:游标是对多行数据结果的一个操作入口,通过遍历游标,我们可以对访问到每行记录的数据,并通过游标取出值,从而进行操作。当select出多行结果集时就会用到cousor.不过游标的缺点就是消耗资源。

项目中遇到的实际问题

declare
  v_sql   varchar2(4000);
  v_table varchar2(4000);
  cursor CUR_DETAILID is
    select tname from tab where tname like 'X\_%' escape '\';
begin
  delete from C_DETAIL_FEATURE_RE;
  open CUR_DETAILID;
  loop
    fetch CUR_DETAILID
      into v_table;
    exit when CUR_DETAILID%notfound;
 
    v_sql := 'insert into C_DETAIL_FEATURE_RE select  S_C_DETAIL_FEATURE_RE.NEXTVAL,D_DETAILID,D_FNO,D_FID FROM ' ||
             v_table;
 
    dbms_output.put_line(v_sql);
    execute immediate v_sql;
    commit;
  end loop;
  close CUR_DETAILID;
  ----去除重复
  delete from C_DETAIL_FEATURE_RE
   where rowid not in (select max(rowid)
                         from C_DETAIL_FEATURE_RE
                        group by (d_fid, d_detailid, d_fno));

commit;
end;

你可能感兴趣的:(oracle)