【ORACLE&DB2】中如何删除一个表中重复数据,仅留其中一行
首先看一下ORACLE中的情况:
delete from test a where rowid < (select max(rowid) from test where col1=a.col1);
但是DB2下由于没有序列rowid,所以不能用如上方法删除,只能实现一个存储过程,示例如下:
test表中只有一个列,其他可以仿照如下实现
CREATE PROCEDURE Delete_same_items (
IN p_a INTEGER)
LANGUAGE SQL
SPECIFIC Delete_same_items
MODIFIES SQL DATA
BEGIN ATOMIC
DECLARE SQLSTATE CHAR(5);
DECLARE v_a INTEGER;
DECLARE c_emp CURSOR WITH HOLD FOR
SELECT a
FROM test where a=p_a
FOR UPDATE OF a;
DECLARE EXIT HANDLER FOR NOT FOUND
SET p_a = NULL;
OPEN c_emp;
FETCH FROM c_emp INTO v_a;
WHILE ( SQLSTATE = '00000' ) DO
FETCH FROM c_emp INTO v_a;
DELETE from test where current of c_emp;
END WHILE;
CLOSE c_emp;
END