/****** Object:  StoredProcedure [dbo].[WZ_UpdateTopCharacter]    Script Date: 05/27/2020 21:10:08 ******/
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[WZ_UpdateTopCharacter]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[WZ_UpdateTopCharacter]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE procedure [dbo].[WZ_UpdateTopCharacter]
as 
BEGIN
	DECLARE @CName varchar(10)
	DECLARE @Top50 int
	SET @Top50 = 0

	Set nocount on
	
	UPDATE Character SET Top50=0, NBB_Resets_0h=0, NBB_Relifes_0h=0, NoResetInDay = 0 WHERE Top50 > 0
	
	DECLARE CUR CURSOR FOR 
		SELECT TOP 500 Name, ROW_NUMBER() OVER(ORDER BY MasterResetCount DESC, ResetCount DESC, cLevel DESC, Resets_Time ASC) AS Top50
		FROM Character
	FOR UPDATE
	OPEN CUR
	FETCH NEXT FROM CUR INTO @CName, @Top50
	WHILE( @@fetch_status <> -1 )
	BEGIN
		IF( @@fetch_status <> -2 )
		BEGIN
			UPDATE Character SET Top50 = @Top50, NBB_Resets_0h = ResetCount, NBB_Relifes_0h = MasterResetCount, Resets_Time_0h = Resets_Time WHERE Name = @CName
		END
		FETCH NEXT FROM CUR INTO @CName, @Top50
	END

	CLOSE CUR
	DEALLOCATE CUR
END
GO