0

A generic way to remove perfectly duplicated rows from all tables owned by a certain user.

1 Answer 1

2

Here's the stored procedure for it. Don't forget to replace 'YOUR_USERNAME_HERE' with your username.

CREATE OR REPLACE PROCEDURE DELETE_DUPLICATES_FROM_DB
IS
  TABLE_COLUMNS VARCHAR2(10000);
  DELETE_STATEMENT VARCHAR2(10000);
  CURSOR ALL_MY_TABLES IS SELECT TABLE_NAME FROM ALL_TABLES WHERE OWNER = 'YOUR_USERNAME_HERE';
BEGIN
  FOR MY_TABLE IN ALL_MY_TABLES 
  LOOP
    SELECT LISTAGG(COLUMN_NAME, ',') WITHIN GROUP (ORDER BY 1) INTO TABLE_COLUMNS FROM USER_TAB_COLUMNS WHERE TABLE_NAME = MY_TABLE.TABLE_NAME;
    DELETE_STATEMENT := 'DELETE FROM ' || MY_TABLE.TABLE_NAME || ' WHERE ROWID NOT IN (SELECT MIN(ROWID) FROM ' || MY_TABLE.TABLE_NAME || ' GROUP BY ' || TABLE_COLUMNS || ');';
    EXECUTE IMMEDIATE DELETE_STATEMENT;
    DBMS_OUTPUT.PUT_LINE(DELETE_STATEMENT);--print the statements we run
  END LOOP;
END;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.