由于导入数据,造成数据库某些表的数据重复,这种情况主要发生在没有设置主键的表。下面是将无主键的表找出并批量去重的sql:
(注意:其中拼接列名用到的listagg()函数 是oracle11g之后的版本才有的)
select 'delete from '||a.TABLE_NAME||' where rowid not in(
select max(rowid) from '||a.TABLE_NAME||' group by '||
(select listagg(COLUMN_NAME,',') within GROUP (order by COLUMN_NAME ) as col
from USER_TAB_COLS b
where a.TABLE_NAME = b.TABLE_NAME)||');'
from USER_TABLES a
where a.TABLE_NAME not in(select TABLE_NAME from user_constraints WHERE constraint_type ='P') and a.num_rows > 0 ;
将查询结果复制到命令窗口执行即可