1. 程式人生 > >mysql儲存過程去除重複資料

mysql儲存過程去除重複資料

CREATE DEFINER=`root`@`%` PROCEDURE `del_incoming_pointer`() BEGIN   /* 定義變數一 */      DECLARE paramId int;      DECLARE paramCode VARCHAR(64);      DECLARE paramTimes varchar(255);      DECLARE paramDevName VARCHAR(255);      DECLARE  _done int default 0;   /* 定義游標 */      DECLARE _Cur CURSOR FOR              SELECT id,spacecraft_code,times,devName FROM zt_file_incoming GROUP BY spacecraft_code,times,devName HAVING COUNT(*)>1;      DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET _done = 1;  /* 開啟游標 */      OPEN _Cur;       FETCH _Cur INTO paramId, paramCode, paramTimes, paramDevName;     /* 迴圈執行 */         WHILE ( _done <> 1) DO           DELETE from zt_file_incoming WHERE id<>paramId and spacecraft_code=paramCode and times=paramTimes and devName=paramDevName;                   COMMIT;         /*遊標向下走一步*/          FETCH _Cur INTO paramId, paramCode, paramTimes, paramDevName;         END WHILE;   /*關閉游標*/      CLOSE _Cur;  END

 paramTimes在資料庫中為時間型別在此處需定義為 varchar