------------------DELETE DUPS IN MIR SQL 2005 DATABASE------------------------------
------------------CODED BY XxEvilDxX------------------------------------------------
------------------Effects TABLES MONGEN, MONSTER, AND MOVEMAPMENT
--================================================================================--
------------------MONGEN Varitables-------------------------------------------------
DECLARE @Count int
DECLARE @MAPNAME nvarchar(50)
DECLARE @X nvarchar(50)
DECLARE @MonName nvarchar(50)
DECLARE @Y nvarchar(50)
DECLARE @RANGE nvarchar(50)
DECLARE @FLDCOUNT nvarchar(50)
DECLARE @GENTIME nvarchar(50)
DECLARE @SMALLGENRATE nvarchar(50)
DECLARE @LOAD nvarchar(50)
------------------MONSTER Varitables-------------------------------------------------
DECLARE @NAME nvarchar(50)
------------------MOVEMAPEVENT Varitables--------------------------------------------
DECLARE @SMAPFILENAME nvarchar(50)
DECLARE @DMAPFILENAME nvarchar(50)
DECLARE @SX nvarchar(50)

--=================================================================================--
--*********************************************************************************--
--=================================================================================--
------------------MONGEN-------------------------------------------------------------
 --Create the Cursor
DECLARE Dublicate_cursorMONGEN CURSOR FAST_FORWARD FOR
--SELECT the FIELD_NAMES(Inside of the tables) count how many have the same names
SELECT FLD_MAPNAME, FLD_X, FLD_Y, FLD_MONNAME, FLD_RANGE, FLD_COUNT, FLD_GENTIME, FLD_SMALLGENRATE, FLD_LOAD, Count(*) - 1
--Point to the TABLE
FROM TBL_MONGEN
--Put them together in a group
GROUP BY FLD_MAPNAME, FLD_X, FLD_Y, FLD_MONNAME, FLD_RANGE, FLD_COUNT, FLD_GENTIME, FLD_SMALLGENRATE, FLD_LOAD
--Any record that is more then 1
HAVING Count(*) > 1
--Open them to work with
OPEN Dublicate_cursorMONGEN
--Get the group and enter there values into the variables
FETCH NEXT FROM Dublicate_cursorMONGEN INTO @MAPNAME, @X, @Y, @MonName, @RANGE, @FLDCOUNT, @GENTIME, @SMALLGENRATE, @LOAD, @Count

WHILE @@FETCH_STATUS = 0
BEGIN
-- Go to the row that has the count number
SET ROWCOUNT @Count
--Delete the record from the data base
DELETE FROM TBL_MONGEN WHERE FLD_MAPNAME = @MAPNAME AND FLD_X = @X AND FLD_Y = @Y AND FLD_MONNAME = @MONNAME AND FLD_RANGE = @RANGE AND FLD_COUNT = @FLDCOUNT AND FLD_GENTIME = @GENTIME AND FLD_SMALLGENRATE = @SMALLGENRATE AND FLD_LOAD = @LOAD 
--Move back to row 0
SET ROWCOUNT 0
--Get the next DUP and load it into the variables 
FETCH NEXT FROM Dublicate_cursorMONGEN INTO @MAPNAME, @X, @Y, @MONNAME, @RANGE, @FLDCOUNT, @GENTIME, @SMALLGENRATE, @LOAD, @Count
END

CLOSE Dublicate_cursorMONGEN
DEALLOCATE Dublicate_cursorMONGEN
SET @Count = 0

------------MONSTER-----------------------------------------------------------------------------------
DECLARE Dublicate_cursorMONSTER CURSOR FAST_FORWARD FOR
SELECT FLD_NAME, Count(*) - 1
FROM TBL_MONSTER
GROUP BY FLD_NAME
HAVING Count(*) > 1

OPEN Dublicate_cursorMONSTER

FETCH NEXT FROM Dublicate_cursorMONSTER INTO @NAME, @Count

WHILE @@FETCH_STATUS = 0
BEGIN

SET ROWCOUNT @Count
DELETE FROM TBL_MONSTER WHERE FLD_NAME = @NAME  
SET ROWCOUNT 0

FETCH NEXT FROM Dublicate_cursorMONSTER INTO @NAME, @Count
END

CLOSE Dublicate_cursorMONSTER
DEALLOCATE Dublicate_cursorMONSTER
Set @Count = 0

------------MOVEMAPEVENT-----------------------------------------------------------------------------------
DECLARE Dublicate_cursorMOVEMAPEVENT CURSOR FAST_FORWARD FOR
SELECT FLD_SMAPFILENAME, FLD_DMAPFILENAME, FLD_SX, Count(*) - 1
FROM TBL_MOVEMAPEVENT
GROUP BY FLD_SMAPFILENAME, FLD_DMAPFILENAME , FLD_SX
HAVING Count(*) > 1

OPEN Dublicate_cursorMOVEMAPEVENT

FETCH NEXT FROM Dublicate_cursorMOVEMAPEVENT INTO @SMAPFILENAME,@DMAPFILENAME, @SX, @Count

WHILE @@FETCH_STATUS = 0
BEGIN

SET ROWCOUNT @Count
DELETE FROM TBL_MOVEMAPEVENT WHERE FLD_SMAPFILENAME = @SMAPFILENAME AND  FLD_DMAPFILENAME = @DMAPFILENAME AND FLD_SX = @SX
SET ROWCOUNT 0

FETCH NEXT FROM Dublicate_cursorMOVEMAPEVENT INTO @SMAPFILENAME,@DMAPFILENAME, @SX, @Count
END

CLOSE Dublicate_cursorMOVEMAPEVENT
DEALLOCATE Dublicate_cursorMOVEMAPEVENT
SET @Count = 0

