May 10 2012

Delete Duplicate Rows Without All Records in Oracle

Category: Administration,Procedure,SQLFatih Acar @ 10:23

If you want to delete duplicate rows but not all rows in table, you can use a procedure. For example, you have 10 duplicate rows and you want to delete 9 rows, then you can use procedure to delete process. For instance, you have a table as tmp_table. tmp_table’s columns are id,name,surname,city,phone. You want to delete duplicate for name,surname,city. You can use below procedure for this process.

Delete Duplicate Rows Procedure

CREATE OR REPLACE PROCEDURE deleteduplicate
IS
BEGIN
FOR duplicates IN ( SELECT max(id) id, name, surname, city
FROM tmp_table GROUP BY name,surname,city
HAVING count(*)>1)

LOOP
DELETE FROM tmp_table t
WHERE t.name = duplicates.name
AND t.surname = duplicates.surname
AND t.city = duplicates.city
AND t.id != duplicates.id;
END LOOP;
COMMIT;
END;

12,066 total views, 10 views today

Tags: Database Administration, Oracle, Stored Procedure

facebook comments:

Leave a Reply

Time limit is exhausted. Please reload CAPTCHA.