USE [qw2021]
GO

/****** Object:  StoredProcedure [dbo].[Migrate_TAF_Import]    Script Date: 26.05.2021 06:45:44 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[Migrate_TAF_Import]
@ortnr int=0,
@return int=0 output,
@statistics varchar(max) output
AS
BEGIN
declare @cnt int 
select @cnt=COUNT(*) from RES_RESULTATE


if @ortnr<>0 begin
   delete from Res_Resultate where ortnr=@ortnr
   delete from Res_Disziplin_Select where ortnr=@ortnr
end


INSERT INTO [dbo].[Res_Resultate]
           ([OrtNr]
           ,[Rang]
           ,[Nachname]
           ,[Vorname]
           ,[Jahrgang]
           ,[Land]
           ,[Verein]
           ,[Lizenz]
           ,[Kategorie_kurzname]
           ,[Kategorie]
           ,[Position]
           ,[Bahn]
           ,[Qualifikation]
           ,[Serie]
           ,[Disziplin]
           ,[Leistung]
           ,[Info]
           ,[Punkte]
           ,[Wind]
           ,[Nicht_Beruecksichtigen]
           ,[Nur_Erdgas]
           ,[Team])
        select ortnr, roundrank,lastname,firstname,yob,nation,clubname,code,class,class,heatnr,lane,0,0,event, 
		   --case when result='n.a.' then '-1' else result end as result,
		   dbo.leistungen_rechnen(event,result),
		      CASE WHEN event='HOC' and eventinfo = '1' then 'O' 
			       when event='HOC' and eventinfo='2' then'XO' 
				   WHEN EVENT='HOC' and eventinfo='3' then 'XXO' else eventinfo  end,
				   SinglePoints,wind,0,0,''
		from [dbo].[Res_TAF_Import]
		where SinglePoints<>'' and SinglePoints > '0' and OrtNr=@ortnr

		-- Diszipllin-Select für Orte festlegen
		exec res_set_disziplin_select @ortnr

		declare @rescount_alt int = @cnt
		declare @rescount_neu int
		select @rescount_neu=count(*) from RES_RESULTATE

		set @cnt=0
		select @cnt=COUNT(*) from RES_RESULTATE where Info<>'O' and Info <>'XO' and Info <>'XXO' and Disziplin='HOC'
		set @return=@cnt
		if @cnt>0 begin
			set @statistics='{Fehler=1,Nicht alle Hochresultate haben einen korrekten Wert im Feld eventinfo='+ltrim(rtrim(@cnt))+','
		end else begin
		    set @statistics='{Fehler=0,'
		end
		set @statistics=@statistics+'Anzahl Resultate vor Upload='+ltrim(rtrim(@rescount_alt))+','
		set @statistics=@statistics+'Anzahl Resultate nach Upload='+ltrim(rtrim(@rescount_neu))+','

		select @cnt=COUNT(*) from Res_TAF_Import where OrtNr=@ortnr
		set @statistics=@statistics+'Anzahl Datensätze='+ltrim(rtrim(@cnt))+','
		select @cnt= count(distinct [event]) from Res_TAF_Import where OrtNr=@ortnr
		set @statistics=@statistics+'Anzahl Disziplinen='+ltrim(rtrim(@cnt))+','
		select @cnt=COUNT(distinct class) from Res_TAF_Import where OrtNr=@ortnr
		set @statistics=@statistics+'Anzahl Kategorien='+ltrim(rtrim(@cnt))

		set @statistics=@statistics+'}'


		print @statistics



END
GO

/****** Object:  StoredProcedure [dbo].[res_resultate_aufbereiten]    Script Date: 26.05.2021 06:45:44 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[res_resultate_aufbereiten] @orte VARCHAR(1024)
	,@u10u12 VARCHAR(1)
	,@resqw INT = 0
	,@resew INT = 0
	,@resortnr INT = 0
AS
BEGIN
	DECLARE @batchstart DATETIME;

	SET @batchstart = GETDATE();

	PRINT 'Start: ' + CONVERT(VARCHAR(12), DATEADD(ms, DATEDIFF(ms, @BatchStart, GETDATE()), 0), 114);

	-----------------------------------------------------------------------------------------------
	-- Work-Tabellen löschen
	-----------------------------------------------------------------------------------------------
	SET NOCOUNT ON;

	TRUNCATE TABLE Res_Resultat_Komplett;

	TRUNCATE TABLE res_temportdaten;

	--IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Res_tmportdaten]') AND type in (N'U'))
	--DROP TABLE [dbo].[Res_tmportdaten]
	-----------------------------------------------------------------------------------------------
	-- Betroffene Orte in temptable übernehmen
	-----------------------------------------------------------------------------------------------
	SELECT *
	INTO #tmp1
	FROM dbo.split(@orte, ';');

	DECLARE @ort VARCHAR(50);
	DECLARE @iortnr INT;
	DECLARE @intortnr INT;
	DECLARE @eintragnr INT;
	DECLARE @ortnr INT;
	DECLARE @rang VARCHAR(50);
	DECLARE @land VARCHAR(50);
	DECLARE @nachname VARCHAR(50);
	DECLARE @vorname VARCHAR(50);
	DECLARE @jahrgang VARCHAR(50);
	DECLARE @verein VARCHAR(50);
	DECLARE @lizenz VARCHAR(50);
	DECLARE @kategorie_kurzname VARCHAR(50);
	DECLARE @kategorie VARCHAR(50);
	DECLARE @position VARCHAR(50);
	DECLARE @bahn VARCHAR(50);
	DECLARE @qualifikation VARCHAR(50);
	DECLARE @serie VARCHAR(50);
	DECLARE @disziplin VARCHAR(50);
	DECLARE @leistung VARCHAR(50);
	DECLARE @punkte VARCHAR(50);
	DECLARE @info VARCHAR(50);
	DECLARE @wind VARCHAR(50);
	DECLARE @nicht_beruecksichtigen INT;
	DECLARE @xsql VARCHAR(1024);
	DECLARE @tblc VARCHAR(1024);
	DECLARE @v INT;
	DECLARE @ne INT;
	DECLARE @team VARCHAR(50);
	DECLARE @sl VARCHAR(50);

	SET @tblc = ' CREATE TABLE [dbo].[_tmpres]([Rang] [int] NULL,	[name] [varchar](101) NULL,	[Jahrgang] [varchar](50) NULL,	[Lizenz] [varchar](50) NULL,';
	SET @tblc = @tblc + '	[Verein] [varchar](50) NULL, [Kategorie_kurzname] [varchar](50) NULL, [Kategorie] [varchar](50) NULL,[Disziplin] [varchar](50) NULL,';

	DECLARE @reseintrag INT;
	DECLARE @reseintrag1 INT;
	DECLARE @oc INT;

	SET @intortnr = 1;
	SET @oc = 0;

	DECLARE xc CURSOR
	FOR
	SELECT *
	FROM #tmp1;

	OPEN xc;

	FETCH NEXT
	FROM xc
	INTO @ort;

	WHILE @@fetch_status = 0
	BEGIN
	
		SELECT @iortnr = ortnr
		FROM dbo.res_ort
		WHERE ort = @ort;
		
		IF @resew <> 1
		BEGIN
			DECLARE xc1 CURSOR
			FOR
			SELECT *
			FROM dbo.res_resultate a
			WHERE a.ortnr = @iortnr
				AND a.nicht_beruecksichtigen <> 1
				AND a.nur_erdgas = 0 -- and disziplin<>'HOCH' 
				AND a.kategorie IN (SELECT kategorie FROM Res_Disziplin_Select WHERE Disziplin=a.Disziplin AND bestenliste=1 and ortnr=a.ortnr)
			ORDER BY a.lizenz
				,a.nachname
				,a.vorname
				,a.punkte;
			--SELECT *
			--FROM dbo.res_resultate
			--WHERE ortnr = @iortnr
			--	AND nicht_beruecksichtigen <> 1
			--	AND nur_erdgas = 0 -- and disziplin<>'HOCH'
			--ORDER BY lizenz
			--	,nachname
			--	,vorname
			--	,punkte
		END;
		ELSE
		BEGIN
			DECLARE xc1 CURSOR
			FOR
			SELECT *
			FROM dbo.res_resultate a
			WHERE ortnr = @iortnr
				AND nicht_beruecksichtigen <> 1 -- and disziplin<>'HOCH'
				AND a.kategorie IN (SELECT kategorie FROM Res_Disziplin_Select WHERE Disziplin=a.Disziplin AND bestenliste=1 and ortnr=a.ortnr)
			ORDER BY lizenz
				,nachname
				,vorname
				,punkte;
		END;

		OPEN xc1;

		FETCH NEXT
		FROM xc1
		INTO @eintragnr
			,@ortnr
			,@rang
			,@nachname
			,@vorname
			,@jahrgang
			,@land
			,@verein
			,@lizenz
			,@kategorie_kurzname
			,@kategorie
			,@position
			,@bahn
			,@qualifikation
			,@serie
			,@disziplin
			,@leistung
			,@info
			,@punkte
			,@wind
			,@nicht_beruecksichtigen
			,@ne
			,@team;

		WHILE @@fetch_status = 0
		BEGIN
			SET @v = 1;

			--if @disziplin='HOCH' begin
			IF @disziplin = 'HOC'
			BEGIN
				SET @v = 0;
				SET @v = (
						SELECT CHARINDEX('O', @info)
						);
			END;

			IF @v > 0
			BEGIN
				IF RTRIM(LTRIM(@lizenz)) <> '0'
					AND RTRIM(LTRIM(@lizenz)) <> ''
				BEGIN
					SELECT @ResEintrag = eintragnr
					FROM dbo.res_resultat_komplett
					WHERE lizenz = @lizenz
						AND disziplin = @disziplin;

					IF @@rowcount = 0
					BEGIN
						INSERT dbo.res_resultat_komplett (
							nachname
							,vorname
							,jahrgang
							,land
							,verein
							,lizenz
							,kategorie_kurzname
							,kategorie
							,disziplin
							,ort1
							,ort2
							,ort3
							,ort4
							,ort5
							,ort6
							,ort7
							,ort8
							,ort9
							,ort10
							,best
							,rang
							,hochversuch
							,Team
							,sl
							)
						VALUES (
							@nachname
							,@vorname
							,@jahrgang
							,@land
							,@verein
							,@lizenz
							,@kategorie_kurzname
							,@kategorie
							,@disziplin
							,0
							,0
							,0
							,0
							,0
							,0
							,0
							,0
							,0
							,0
							,''
							,0
							,''
							,@team
							,@leistung
							);

						SELECT TOP 1 @reseintrag = eintragnr
						FROM dbo.res_resultat_komplett
						ORDER BY eintragnr DESC;
					END;
				END;
				ELSE
				BEGIN
					SELECT @ResEintrag = eintragnr
					FROM dbo.res_resultat_komplett
					WHERE nachname = @nachname
						AND vorname = @vorname
						AND jahrgang = @jahrgang
						AND kategorie = @kategorie
						AND disziplin = @disziplin;

					IF @@rowcount = 0
					BEGIN
						INSERT dbo.res_resultat_komplett (
							nachname
							,vorname
							,jahrgang
							,land
							,verein
							,lizenz
							,kategorie_kurzname
							,kategorie
							,disziplin
							,ort1
							,ort2
							,ort3
							,ort4
							,ort5
							,ort6
							,ort7
							,ort8
							,ort9
							,ort10
							,best
							,rang
							,hochversuch
							,team
							,sl
							)
						VALUES (
							@nachname
							,@vorname
							,@jahrgang
							,@land
							,@verein
							,@lizenz
							,@kategorie_kurzname
							,@kategorie
							,@disziplin
							,0
							,0
							,0
							,0
							,0
							,0
							,0
							,0
							,0
							,0
							,''
							,0
							,''
							,@team
							,@leistung
							);

						SELECT TOP 1 @reseintrag = eintragnr
						FROM dbo.res_resultat_komplett
						ORDER BY eintragnr DESC;
					END;
				END;

				INSERT INTO dbo.res_temportdaten (
					eintragparent
					,POSITION
					,bahn
					,qualifikation
					,serie
					,leistung
					,info
					,punkte
					,wind
					,leistung_aufbereitet
					)
				VALUES (
					 @reseintrag
					,@position
					,@bahn
					,@qualifikation
					,@serie
					,@leistung
					,@info
					,@punkte
					,@wind
					,dbo.get_leistung(@leistung, @disziplin)
					);

				SELECT TOP 1 @reseintrag1 = eintragnr
				FROM dbo.res_temportdaten
				ORDER BY eintragnr DESC;

				SET @sl = @leistung;

				--shu 20100527; HOCH ebenfalls ausschliessen
				IF SUBSTRING(@disziplin, 1, 3) <> '1K0'
					AND SUBSTRING(@disziplin, 1, 3) <> '600'
					AND @disziplin <> 'HOC'
				BEGIN
					--         if substring(@disziplin,1,4)<>'1000' and substring(@disziplin,1,3)<>'600' and @disziplin <> 'HOCH'begin 
					IF SUBSTRING(@leistung, 1, 1) <> '-'
					BEGIN
						SET @sl = '-' + @punkte;
					END;
				END;

				-- 20100527 - SHU Speziell für HOXH
				--if @disziplin='HOCH' begin
				IF @disziplin = 'HOC'
				BEGIN
					UPDATE dbo.res_resultat_komplett
					SET sl = @sl
					WHERE eintragnr = @reseintrag
						AND @sl > sl;

					DECLARE @sl1 VARCHAR(50);

					SELECT @sl1 = sl
					FROM dbo.Res_Resultat_Komplett
					WHERE eintragnr = @reseintrag;

					IF @sl = @sl1
					BEGIN
						SET @xsql = 'ort' + RTRIM(LTRIM(STR(@intortnr)));
						SET @xsql = 'update dbo.res_resultat_komplett set ' + @xsql + '=' + LTRIM(RTRIM(STR(@reseintrag1))) + ' where eintragnr=' + LTRIM(RTRIM(STR(@reseintrag)));

						EXECUTE (@xsql);
					END;
					ELSE
					BEGIN
						-- 20100615 korrektur hoch
						SET @xsql = 'ort' + RTRIM(LTRIM(STR(@intortnr)));
						SET @xsql = 'update dbo.res_resultat_komplett set ' + @xsql + '=' + LTRIM(RTRIM(STR(@reseintrag1))) + ' where eintragnr=' + LTRIM(RTRIM(STR(@reseintrag)));

						EXECUTE (@xsql);
					END;
				END;
				ELSE
				BEGIN
					UPDATE dbo.res_resultat_komplett
					SET sl = @sl
					WHERE eintragnr = @reseintrag
						AND @sl < sl;

					SET @xsql = 'ort' + RTRIM(LTRIM(STR(@intortnr)));
					SET @xsql = 'update dbo.res_resultat_komplett set ' + @xsql + '=' + LTRIM(RTRIM(STR(@reseintrag1))) + ' where eintragnr=' + LTRIM(RTRIM(STR(@reseintrag)));

					EXECUTE (@xsql);
				END;
			END;

			FETCH NEXT
			FROM xc1
			INTO @eintragnr
				,@ortnr
				,@rang
				,@nachname
				,@vorname
				,@jahrgang
				,@land
				,@verein
				,@lizenz
				,@kategorie_kurzname
				,@kategorie
				,@position
				,@bahn
				,@qualifikation
				,@serie
				,@disziplin
				,@leistung
				,@info
				,@punkte
				,@wind
				,@nicht_beruecksichtigen
				,@ne
				,@team;
		END;

		CLOSE xc1;

		DEALLOCATE xc1;

		SET @intortnr = @intortnr + 1;

		FETCH NEXT
		FROM xc
		INTO @ort;
	END;

	CLOSE xc;

	DEALLOCATE xc;

	---- shu 20100527
	PRINT 'Vor Res 1: ' + CONVERT(VARCHAR(12), DATEADD(ms, DATEDIFF(ms, @BatchStart, GETDATE()), 0), 114);

	EXECUTE dbo.res_resultate_aufbereiten_1 @u10u12;

	PRINT 'Nach Res 1: ' + CONVERT(VARCHAR(12), DATEADD(ms, DATEDIFF(ms, @BatchStart, GETDATE()), 0), 114);

	WHILE @oc < 9
	BEGIN
		SET @tblc = @tblc + '[F' + LTRIM(RTRIM(@oc)) + '] [varchar] (50) null,';
		SET @oc = @oc + 1;
	END;

	IF EXISTS (
			SELECT *
			FROM sys.objects
			WHERE OBJECT_ID = OBJECT_ID(N'[dbo].[_tmpres]')
				AND TYPE IN (N'U')
			)
		DROP TABLE [dbo].[_tmpres];

	SET @tblc = @tblc + '[Best] [varchar](50) NULL,[BestPunkte] [int] NULL, [Land] [Varchar] (50) NULL, [hochversuch] [varchar] (50) null, [hochbest] [varchar] (50) null, [team] [varchar] (50) null) ON [PRIMARY]';

	EXECUTE (@tblc);

	UPDATE dbo.res_temportdaten
	SET leistung_aufbereitet = '0'
		,leistung = 0
	WHERE punkte = 0;

	INSERT dbo._tmpres
	SELECT dbo.Res_Resultat_Komplett.Rang
		,dbo.Res_Resultat_Komplett.Nachname + ' ' + dbo.Res_Resultat_Komplett.Vorname AS name
		,dbo.Res_Resultat_Komplett.Jahrgang
		,dbo.Res_Resultat_Komplett.Lizenz
		,dbo.Res_Resultat_Komplett.Verein
		,dbo.Res_Resultat_Komplett.Kategorie_kurzname
		,dbo.Res_Resultat_Komplett.Kategorie
		,dbo.Res_Resultat_Komplett.Disziplin
		,dbo.res_temportdaten.Leistung_Aufbereitet AS Lo1
		,res_temportdaten_1.Leistung_Aufbereitet AS Lo2
		,res_temportdaten_2.Leistung_Aufbereitet AS Lo3
		,res_temportdaten_3.Leistung_Aufbereitet AS Lo4
		,res_temportdaten_4.Leistung_Aufbereitet AS Lo5
		,res_temportdaten_5.Leistung_Aufbereitet AS Lo6
		,res_temportdaten_6.Leistung_Aufbereitet AS Lo7
		,res_temportdaten_7.Leistung_Aufbereitet AS Lo8
		,res_temportdaten_8.Leistung_Aufbereitet AS Lo9
		,dbo.Res_Resultat_Komplett.Best
		,dbo.Res_Resultat_Komplett.BestPunkte
		,dbo.res_resultat_komplett.land
		,dbo.Res_Resultat_Komplett.HochVersuch
		,dbo.Res_Resultat_Komplett.Hochbest
		,dbo.Res_Resultat_Komplett.Team
	FROM dbo.Res_Resultat_Komplett
	LEFT OUTER JOIN dbo.res_temportdaten AS res_temportdaten_7 ON dbo.Res_Resultat_Komplett.ort8 = res_temportdaten_7.EintragNr
	LEFT OUTER JOIN dbo.res_temportdaten AS res_temportdaten_6 ON dbo.Res_Resultat_Komplett.ort7 = res_temportdaten_6.EintragNr
	LEFT OUTER JOIN dbo.res_temportdaten AS res_temportdaten_4 ON dbo.Res_Resultat_Komplett.ort5 = res_temportdaten_4.EintragNr
	LEFT OUTER JOIN dbo.res_temportdaten AS res_temportdaten_1 ON dbo.Res_Resultat_Komplett.ort2 = res_temportdaten_1.EintragNr
	LEFT OUTER JOIN dbo.res_temportdaten AS res_temportdaten_2 ON dbo.Res_Resultat_Komplett.ort3 = res_temportdaten_2.EintragNr
	LEFT OUTER JOIN dbo.res_temportdaten AS res_temportdaten_3 ON dbo.Res_Resultat_Komplett.ort4 = res_temportdaten_3.EintragNr
	LEFT OUTER JOIN dbo.res_temportdaten ON dbo.Res_Resultat_Komplett.ort1 = dbo.res_temportdaten.EintragNr
	LEFT OUTER JOIN dbo.res_temportdaten AS res_temportdaten_5 ON dbo.Res_Resultat_Komplett.ort6 = res_temportdaten_5.EintragNr
	LEFT OUTER JOIN dbo.res_temportdaten AS res_temportdaten_8 ON dbo.Res_Resultat_Komplett.ort9 = res_temportdaten_8.EintragNr;

	UPDATE dbo._tmpres
	SET best = 0
	WHERE best = '-0.01';

	UPDATE dbo._tmpres
	SET best = 0
	WHERE best = '-0.00';

	--shu 20100527
	--update dbo._tmpres set best=0 where bestpunkte='0'
	UPDATE dbo._tmpres
	SET best = 0
	WHERE bestpunkte = '0'
		AND best IS NULL;

	IF @resqw = 0
		AND @resew = 0
	BEGIN
		DECLARE @qw VARCHAR(MAX);
		DECLARE @rangliste VARCHAR(MAX);
		DECLARE @reskat VARCHAR(MAX);

		DECLARE xc CURSOR
		FOR
		SELECT qw
			,Kategorie
			,rangliste
		FROM Disziplin_Mapping ORDER BY Kategorie DESC;

		OPEN xc;

		FETCH NEXT
		FROM xc
		INTO @qw
			,@reskat
			,@rangliste;

		WHILE @@FETCH_STATUS = 0
		BEGIN
			IF @reskat='' BEGIN

			UPDATE dbo._tmpres
				SET Disziplin = @rangliste
			WHERE Disziplin = @qw; 
			END; ELSE BEGIN
			UPDATE dbo._tmpres
				SET Disziplin = @rangliste
			WHERE Disziplin = @qw AND kategorie=@reskat;
			END;


			FETCH NEXT
			FROM xc
			INTO @qw
				,@reskat
				,@rangliste;
		END;

		CLOSE xc;

		DEALLOCATE xc;

		SELECT *
		FROM dbo._tmpres;
	END;
	ELSE
	BEGIN
		PRINT 'res 2 start: ' + CONVERT(VARCHAR(12), DATEADD(ms, DATEDIFF(ms, @BatchStart, GETDATE()), 0), 114);

		EXECUTE dbo.res_resultate_aufbereiten_2 @u10u12
			,@resqw
			,@resew
			,@resortnr;

		PRINT 'res 2 ende: ' + CONVERT(VARCHAR(12), DATEADD(ms, DATEDIFF(ms, @BatchStart, GETDATE()), 0), 114);
	END;

	PRINT 'Ende: ' + CONVERT(VARCHAR(12), DATEADD(ms, DATEDIFF(ms, @BatchStart, GETDATE()), 0), 114);
END;
GO

/****** Object:  StoredProcedure [dbo].[res_resultate_aufbereiten_1]    Script Date: 26.05.2021 06:45:44 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[res_resultate_aufbereiten_1] @u10u12 VARCHAR(1)
AS
CREATE TABLE #tmpd (
	[ortnr] [int] NULL
	,[ortpt] [int] NULL
	,[hochbest] VARCHAR(50)
	) ON [PRIMARY]

DECLARE @pnr INT
DECLARE @onr1 INT
DECLARE @opt1 INT
DECLARE @onr2 INT
DECLARE @opt2 INT
DECLARE @onr3 INT
DECLARE @opt3 INT
DECLARE @onr4 INT
DECLARE @opt4 INT
DECLARE @onr5 INT
DECLARE @opt5 INT
DECLARE @onr6 INT
DECLARE @opt6 INT
DECLARE @onr7 INT
DECLARE @opt7 INT
DECLARE @onr8 INT
DECLARE @opt8 INT
DECLARE @onr9 INT
DECLARE @opt9 INT
DECLARE @info1 VARCHAR(50)
DECLARE @info2 VARCHAR(50)
DECLARE @info3 VARCHAR(50)
DECLARE @info4 VARCHAR(50)
DECLARE @info5 VARCHAR(50)
DECLARE @info6 VARCHAR(50)
DECLARE @info7 VARCHAR(50)
DECLARE @info8 VARCHAR(50)
DECLARE @info9 VARCHAR(50)
DECLARE @onrbest INT
DECLARE @optbest INT
DECLARE @l VARCHAR(50)
DECLARE @disx VARCHAR(50)
DECLARE @info VARCHAR(50)
DECLARE @hv VARCHAR(50)
DECLARE @hb VARCHAR(50)
DECLARE @hb1 VARCHAR(50)
DECLARE @sl INT
DECLARE @batchstart DATETIME

SET @batchstart = GETDATE()

PRINT '   Res 1 Start: ' + CONVERT(VARCHAR(12), DATEADD(ms, DATEDIFF(ms, @BatchStart, getdate()), 0), 114)

DECLARE x1 CURSOR
FOR
SELECT dbo.Res_Resultat_Komplett.EintragNr
	,dbo.Res_Resultat_Komplett.Disziplin
	,res_temportdaten_2.EintragNr AS Oort1Nr
	,res_temportdaten_2.Punkte AS Ort1Pt
	,res_temportdaten_2.Info AS Info1
	,res_temportdaten_1.EintragNr AS Ort2Nr
	,res_temportdaten_1.Punkte AS Ort2Pt
	,res_temportdaten_1.Info AS Info2
	,res_temportdaten_3.EintragNr AS Ort3Nr
	,res_temportdaten_3.Punkte AS Ort3Pt
	,res_temportdaten_3.Info AS Info3
	,res_temportdaten_7.EintragNr AS Ort4Nr
	,res_temportdaten_7.Punkte AS Ort4Pt
	,res_temportdaten_7.Info AS Info4
	,res_temportdaten_4.EintragNr AS Ort5Nr
	,res_temportdaten_4.Punkte AS Ort5Pt
	,res_temportdaten_4.Info AS Info5
	,res_temportdaten_5.EintragNr AS Ort6Nr
	,res_temportdaten_5.Punkte AS Ort6Pt
	,res_temportdaten_5.Info AS Info6
	,res_temportdaten_6.EintragNr AS OrtNr7
	,res_temportdaten_6.Punkte AS OrtPt7
	,res_temportdaten_6.Info AS Info7
	,dbo.res_temportdaten.EintragNr AS OrtNr8
	,dbo.res_temportdaten.Punkte AS OrtPt8
	,dbo.res_temportdaten.Info AS Info8
	,res_temportdaten_8.EintragNr AS OrtNr9
	,res_temportdaten_8.Punkte AS OrtPt9
	,res_temportdaten_8.Info AS Info9

FROM dbo.Res_Resultat_Komplett
LEFT OUTER JOIN dbo.res_temportdaten ON dbo.Res_Resultat_Komplett.ort8 = dbo.res_temportdaten.EintragNr
LEFT OUTER JOIN dbo.res_temportdaten AS res_temportdaten_6 ON dbo.Res_Resultat_Komplett.ort7 = res_temportdaten_6.EintragNr
LEFT OUTER JOIN dbo.res_temportdaten AS res_temportdaten_4 ON dbo.Res_Resultat_Komplett.ort5 = res_temportdaten_4.EintragNr
LEFT OUTER JOIN dbo.res_temportdaten AS res_temportdaten_5 ON dbo.Res_Resultat_Komplett.ort6 = res_temportdaten_5.EintragNr
LEFT OUTER JOIN dbo.res_temportdaten AS res_temportdaten_7 ON dbo.Res_Resultat_Komplett.ort4 = res_temportdaten_7.EintragNr
LEFT OUTER JOIN dbo.res_temportdaten AS res_temportdaten_3 ON dbo.Res_Resultat_Komplett.ort3 = res_temportdaten_3.EintragNr
LEFT OUTER JOIN dbo.res_temportdaten AS res_temportdaten_1 ON dbo.Res_Resultat_Komplett.ort2 = res_temportdaten_1.EintragNr
LEFT OUTER JOIN dbo.res_temportdaten AS res_temportdaten_2 ON dbo.Res_Resultat_Komplett.ort1 = res_temportdaten_2.EintragNr
LEFT OUTER JOIN dbo.res_temportdaten AS res_temportdaten_8 ON dbo.Res_Resultat_Komplett.ort9 = res_temportdaten_8.EintragNr

OPEN x1

FETCH NEXT
FROM x1
INTO @pnr
	,@disx
	,@onr1
	,@opt1
	,@info1
	,@onr2
	,@opt2
	,@info2
	,@onr3
	,@opt3
	,@info3
	,@onr4
	,@opt4
	,@info4
	,@onr5
	,@opt5
	,@info5
	,@onr6
	,@opt6
	,@info6
	,@onr7
	,@opt7
	,@info7
	,@onr8
	,@opt8
	,@info8
	,@onr9
	,@opt9
	,@info9

WHILE @@fetch_status = 0
BEGIN
	--if @disx='HOCH' begin
	IF @disx = 'HOC'
	BEGIN
		INSERT #tmpd (
			ortnr
			,ortpt
			,[hochbest]
			)
		VALUES (
			@onr1
			,@opt1
			,@info1
			)

		INSERT #tmpd (
			ortnr
			,ortpt
			,[hochbest]
			)
		VALUES (
			@onr2
			,@opt2
			,@info2
			)

		INSERT #tmpd (
			ortnr
			,ortpt
			,[hochbest]
			)
		VALUES (
			@onr3
			,@opt3
			,@info3
			)

		INSERT #tmpd (
			ortnr
			,ortpt
			,[hochbest]
			)
		VALUES (
			@onr4
			,@opt4
			,@info4
			)

		INSERT #tmpd (
			ortnr
			,ortpt
			,[hochbest]
			)
		VALUES (
			@onr5
			,@opt5
			,@info5
			)

		INSERT #tmpd (
			ortnr
			,ortpt
			,[hochbest]
			)
		VALUES (
			@onr6
			,@opt6
			,@info6
			)

		INSERT #tmpd (
			ortnr
			,ortpt
			,[hochbest]
			)
		VALUES (
			@onr7
			,@opt7
			,@info7
			)

		INSERT #tmpd (
			ortnr
			,ortpt
			,[hochbest]
			)
		VALUES (
			@onr8
			,@opt8
			,@info8
			)
					INSERT #tmpd (
			ortnr
			,ortpt
			,[hochbest]
			)
		VALUES (
			@onr9
			,@opt9
			,@info9
			)
	END
	ELSE
	BEGIN
		INSERT #tmpd (
			ortnr
			,ortpt
			,[hochbest]
			)
		VALUES (
			@onr1
			,@opt1
			,'xxxx'
			)

		INSERT #tmpd (
			ortnr
			,ortpt
			,[hochbest]
			)
		VALUES (
			@onr2
			,@opt2
			,'xxxx'
			)

		INSERT #tmpd (
			ortnr
			,ortpt
			,[hochbest]
			)
		VALUES (
			@onr3
			,@opt3
			,'xxxx'
			)

		INSERT #tmpd (
			ortnr
			,ortpt
			,[hochbest]
			)
		VALUES (
			@onr4
			,@opt4
			,'xxxx'
			)

		INSERT #tmpd (
			ortnr
			,ortpt
			,[hochbest]
			)
		VALUES (
			@onr5
			,@opt5
			,'xxxx'
			)

		INSERT #tmpd (
			ortnr
			,ortpt
			,[hochbest]
			)
		VALUES (
			@onr6
			,@opt6
			,'xxxx'
			)

		INSERT #tmpd (
			ortnr
			,ortpt
			,[hochbest]
			)
		VALUES (
			@onr7
			,@opt7
			,'xxxx'
			)

		INSERT #tmpd (
			ortnr
			,ortpt
			,[hochbest]
			)
		VALUES (
			@onr8
			,@opt8
			,'xxxx'
			)
		INSERT #tmpd (
			ortnr
			,ortpt
			,[hochbest]
			)
		VALUES (
			@onr9
			,@opt9
			,'xxxx'
			)
	END

	SET @hv = ''

	SELECT TOP 1 @onrbest = ortnr
		,@optbest = ortpt
		,@hb = dbo.get_hochversuche(hochbest)
	FROM #tmpd
	ORDER BY ortpt DESC
		,hochbest ASC

	SELECT @l = leistung_aufbereitet
	FROM dbo.res_temportdaten
	WHERE eintragnr = @onrbest

	--if upper(@disx)='HOCH' begin
	IF upper(@disx) = 'HOC'
	BEGIN
		SET @info = ''

		SELECT @info = dbo.get_hochversuche(info)
		FROM dbo.res_temportdaten
		WHERE eintragnr = @onr1

		IF @info <> ''
		BEGIN
			IF @hv <> ''
				SET @hv = @hv + ';'
			SET @hv = @hv + @info
		END

		SET @info = ''

		SELECT @info = dbo.get_hochversuche(info)
		FROM dbo.res_temportdaten
		WHERE eintragnr = @onr2

		IF @info <> ''
		BEGIN
			IF @hv <> ''
				SET @hv = @hv + ';'
			SET @hv = @hv + @info
		END

		SET @info = ''

		SELECT @info = dbo.get_hochversuche(info)
		FROM dbo.res_temportdaten
		WHERE eintragnr = @onr3

		IF @info <> ''
		BEGIN
			IF @hv <> ''
				SET @hv = @hv + ';'
			SET @hv = @hv + @info
		END

		SET @info = ''

		SELECT @info = dbo.get_hochversuche(info)
		FROM dbo.res_temportdaten
		WHERE eintragnr = @onr4

		IF @info <> ''
		BEGIN
			IF @hv <> ''
				SET @hv = @hv + ';'
			SET @hv = @hv + @info
		END

		SET @info = ''

		SELECT @info = dbo.get_hochversuche(info)
		FROM dbo.res_temportdaten
		WHERE eintragnr = @onr5

		IF @info <> ''
		BEGIN
			IF @hv <> ''
				SET @hv = @hv + ';'
			SET @hv = @hv + @info
		END

		SET @info = ''

		SELECT @info = dbo.get_hochversuche(info)
		FROM dbo.res_temportdaten
		WHERE eintragnr = @onr6

		IF @info <> ''
		BEGIN
			IF @hv <> ''
				SET @hv = @hv + ';'
			SET @hv = @hv + @info
		END

		SET @info = ''

		SELECT @info = dbo.get_hochversuche(info)
		FROM dbo.res_temportdaten
		WHERE eintragnr = @onr7

		IF @info <> ''
		BEGIN
			IF @hv <> ''
				SET @hv = @hv + ';'
			SET @hv = @hv + @info
		END

		SET @info = ''

		SELECT @info = dbo.get_hochversuche(info)
		FROM dbo.res_temportdaten
		WHERE eintragnr = @onr8

		IF @info <> ''
		BEGIN
			IF @hv <> ''
				SET @hv = @hv + ';'
			SET @hv = @hv + @info
		END

		SELECT @info = dbo.get_hochversuche(info)
		FROM dbo.res_temportdaten
		WHERE eintragnr = @onr9

		IF @info <> ''
		BEGIN
			IF @hv <> ''
				SET @hv = @hv + ';'
			SET @hv = @hv + @info
		END
	END

	UPDATE dbo.res_resultat_komplett
	SET best = @l
		,bestpunkte = @optbest
		,hochversuch = @hv
		,hochbest = @hb
	WHERE eintragnr = @pnr

	TRUNCATE TABLE #tmpd

	FETCH NEXT
	FROM x1
	INTO @pnr
		,@disx
		,@onr1
		,@opt1
		,@info1
		,@onr2
		,@opt2
		,@info2
		,@onr3
		,@opt3
		,@info3
		,@onr4
		,@opt4
		,@info4
		,@onr5
		,@opt5
		,@info5
		,@onr6
		,@opt6
		,@info6
		,@onr7
		,@opt7
		,@info7
		,@onr8
		,@opt8
		,@info8
		,@onr9
		,@opt9
		,@info9

END

CLOSE x1

DEALLOCATE x1

PRINT '   Res1 Ende Teil 1: ' + CONVERT(VARCHAR(12), DATEADD(ms, DATEDIFF(ms, @BatchStart, getdate()), 0), 114)

DECLARE @rang INT
DECLARE @kat VARCHAR(50)
DECLARE @kat1 VARCHAR(50)
DECLARE @dis VARCHAR(50)
DECLARE @dis1 VARCHAR(50)
DECLARE @pt FLOAT
DECLARE @pt1 FLOAT
DECLARE @enr INT
DECLARE @rang1 INT

SET @kat = ''
SET @kat1 = ''
SET @dis = ''
SET @dis1 = ''
SET @pt = 0
SET @pt1 = 0
SET @rang = 0
SET @rang1 = 1
SET @hb = ''
SET @hb1 = ''

IF @u10u12 = 'J'
BEGIN
	UPDATE dbo.res_resultat_komplett
	SET kategorie_kurzname = 'U12M'
	WHERE kategorie_kurzname = 'U10M'

	UPDATE dbo.res_resultat_komplett
	SET kategorie_kurzname = 'U12W'
	WHERE kategorie_kurzname = 'U10W'
END

DECLARE xc CURSOR
FOR
SELECT EintragNr
	,Kategorie_kurzname
	,Disziplin
	,sl
	,hochbest
FROM Res_Resultat_Komplett
ORDER BY Kategorie_kurzname
	,Disziplin
	,BestPunkte DESC
	,sl ASC
	,hochbest ASC
	,hochversuch ASC

OPEN xc

FETCH NEXT
FROM xc
INTO @enr
	,@kat
	,@dis
	,@pt
	,@hb

WHILE @@fetch_status = 0
BEGIN
	IF @kat1 <> @kat
	BEGIN
		SET @kat1 = @kat
		SET @rang = 0
		SET @rang1 = 1
	END

	IF @dis1 <> @dis
	BEGIN
		SET @dis1 = @dis
		SET @rang = 0
		SET @rang1 = 1
	END

	IF @pt <> @pt1
	BEGIN
		SET @pt1 = @pt
		SET @rang = @rang1
	END

	--if @dis='HOCH' and (@hb <> @hb1) begin
	IF @dis = 'HOC'
		AND (@hb <> @hb1)
	BEGIN
		SET @hb1 = @hb
		SET @rang = @rang1
	END

	SET @rang1 = @rang1 + 1

	UPDATE dbo.res_resultat_komplett
	SET rang = @rang
	WHERE eintragnr = @enr

	FETCH NEXT
	FROM xc
	INTO @enr
		,@kat
		,@dis
		,@pt
		,@hb
END

CLOSE xc

DEALLOCATE xc

PRINT '   Res1 End Teil 2: ' + CONVERT(VARCHAR(12), DATEADD(ms, DATEDIFF(ms, @BatchStart, getdate()), 0), 114)
GO

/****** Object:  StoredProcedure [dbo].[res_resultate_aufbereiten_2]    Script Date: 26.05.2021 06:45:44 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO





-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[res_resultate_aufbereiten_2] 
@u10u12 varchar(1),
@resqw int = 0,
@resew int = 0,
@resortnr int=0
AS
BEGIN

CREATE TABLE #tmpa(
	[Rang] [int] NULL,
	[name] [varchar](101) NULL,
	[Jahrgang] [varchar](50) NULL,
	[Lizenz] [varchar](50) NULL,
	[Verein] [varchar](50) NULL,
	[Kategorie_Kurzname] [varchar](50) NULL,
	[Kategorie] [varchar](50) NULL,
	[Disziplin] [varchar](50) NULL,
	[F0] [varchar](50) NULL,
	[F1] [varchar](50) NULL,
	[F2] [varchar](50) NULL,
	[F3] [varchar](50) NULL,
	[F4] [varchar](50) NULL,
	[F5] [varchar](50) NULL,
	[F6] [varchar](50) NULL,
	[F7] [varchar](50) NULL,
	[Best] [varchar](50) NULL,
	[BestPunkte] [int] NULL,
	[Land] [varchar](50) NULL,
	[hochversuch] [varchar](50) NULL,
	[hochbest] [varchar](50) NULL,
	[team] [varchar](50) NULL,
    [pk] [int] IDENTITY(1,1) NOT NULL
) ON [PRIMARY]


CREATE TABLE #tmpr(
    [Rang] [int] NULL,
	[name] [varchar](101) NULL,
	[Jahrgang] [varchar](50) NULL,
	[Lizenz] [varchar](50) NULL,
	[Verein] [varchar](50) NULL,
	[Kategorie_Kurzname] [varchar](50) NULL,
	[Kategorie] [varchar](50) NULL,
	[Dis1] [varchar](50) NULL,
	[Lei1] [varchar](50) NULL,
	[Pun1] [varchar](50) NULL,
	[Dis2] [varchar](50) NULL,
	[Lei2] [varchar](50) NULL,
	[Pun2] [varchar](50) NULL,
	[Dis3] [varchar](50) NULL,
	[Lei3] [varchar](50) NULL,
	[Pun3] [varchar](50) NULL,
	[Dis4] [varchar](50) NULL,
	[Lei4] [varchar](50) NULL,
	[Pun4] [varchar](50) NULL,
	[Dis5] [varchar](50) NULL,
	[Lei5] [varchar](50) NULL,
	[Pun5] [varchar](50) NULL,
	[Dis6] [varchar](50) NULL,
	[Lei6] [varchar](50) NULL,
	[Pun6] [varchar](50) NULL,
	[Best] [varchar](50) NULL,
    [team] [varchar](50) NULL,
	[pk] [int] IDENTITY(1,1) NOT NULL

) ON [PRIMARY]


   if @resqw=1 begin

      goto qw
   end
   if @resew=1 begin
      goto ew
   end

return

qw:
   insert into #tmpa SELECT dbo._tmpres.Rang, dbo._tmpres.name, dbo._tmpres.Jahrgang, dbo._tmpres.Lizenz, dbo._tmpres.Verein, dbo._tmpres.Kategorie_kurzname, dbo._tmpres.Kategorie, 
          dbo._tmpres.Disziplin, dbo._tmpres.F0, dbo._tmpres.F1, dbo._tmpres.F2, dbo._tmpres.F3, dbo._tmpres.F4, dbo._tmpres.F5, dbo._tmpres.F6, dbo._tmpres.F7, 
          dbo._tmpres.Best, dbo._tmpres.BestPunkte, dbo._tmpres.Land, dbo._tmpres.hochversuch, dbo._tmpres.hochbest, dbo._tmpres.team
   FROM   dbo.Res_Disziplin_Select INNER JOIN
          dbo._tmpres ON dbo.Res_Disziplin_Select.Disziplin = dbo._tmpres.Disziplin  AND dbo.Res_Disziplin_Select.Kategorie = dbo._tmpres.Kategorie_kurzname
   WHERE  (dbo.Res_Disziplin_Select.QW = 1) AND (dbo.Res_Disziplin_Select.OrtNr = @resortnr)

   declare @dis varchar(50)
   declare @name varchar(50)
   declare @jg varchar(50)
   declare @lic varchar(50)
   declare @verein varchar(50)
   declare @kat_kurz varchar(50)
   declare @kat varchar(50)
   declare @dis1 varchar(50)
   declare @cnt int
   declare @katalt varchar(50)
   declare @disalt varchar(50)
   declare @fc int
   declare @lei varchar(50)
   declare @pun varchar(50)
   declare @team varchar(50)
   --20100525
   update #tmpa set team='Einzelstarter' where team like 'Einzelstarter%'

   set @katalt=''
   set @disalt=''
   set @fc =0
   declare xc cursor for select kategorie, disziplin from dbo.res_disziplin_select where ortnr=@resortnr and qw=1 order by kategorie, disziplin
   open xc
   fetch next from xc into @kat,@dis
   while @@fetch_status=0 begin
      if @kat<>@katalt begin
         set @fc=0
         set @katalt=@kat
      end
      if @dis<>@disalt
         set @fc=@fc+1
      declare xc1 cursor for select [name],[Jahrgang],[Lizenz],[Verein],[Kategorie_kurzname],[Kategorie],[Disziplin], best, bestpunkte, team
                              from #tmpa where kategorie_kurzname=@kat and disziplin=@dis
      open xc1
      fetch next from xc1 into @name, @jg, @lic, @verein, @kat_kurz, @kat, @dis1, @lei, @pun, @team
      while @@fetch_status = 0 begin
         set @cnt=(Select pk from #tmpr where name=@name and jahrgang=@jg and lizenz=@lic)
         if @cnt=0 or @cnt is null begin
            insert #tmpr ([name],[Jahrgang],[Lizenz],[Verein],[Kategorie_kurzname],[Kategorie],[Dis1],[Lei1],[Pun1],[Dis2],[Lei2],[Pun2],[Dis3],[Lei3],[Pun3],[Dis4],[Lei4],[Pun4],[Dis5],[Lei5],[Pun5],[Dis6],[Lei6],[Pun6],[Best], team)
            values (@name, @jg, @lic, @verein, @kat_kurz, @kat, '','','','','','','','','','','','','','','','','','',0,@team)
            set @cnt=(select top 1 pk from #tmpr order by pk desc)
         end
         if @fc=1 begin
            update #tmpr set lei1=@lei,dis1=@dis1, pun1=@pun where pk=@cnt
         end
         if @fc=2 begin
            update #tmpr set lei2=@lei,dis2=@dis1,  pun2=@pun where pk=@cnt
         end
         if @fc=3 begin
            update #tmpr set lei3=@lei,dis3=@dis1,  pun3=@pun where pk=@cnt
         end
         if @fc=4 begin
            update #tmpr set lei4=@lei,dis4=@dis1,  pun4=@pun where pk=@cnt
         end
         if @fc=5 begin
            update #tmpr set lei5=@lei,dis5=@dis1,  pun5=@pun where pk=@cnt
         end
         if @fc=6 begin
            update #tmpr set lei6=@lei,dis6=@dis1,  pun6=@pun where pk=@cnt
         end

         fetch next from xc1 into @name, @jg, @lic, @verein, @kat_kurz, @kat, @dis1,@lei, @pun, @team
      end
      close xc1
      deallocate xc1
         if @fc=1 begin
            update #tmpr set dis1=@dis1 where dis1='' and kategorie=@kat
         end
         if @fc=2 begin
            update #tmpr set dis2=@dis1 where dis2='' and kategorie=@kat
         end
         if @fc=3 begin
            update #tmpr set dis3=@dis1 where dis3='' and kategorie=@kat
         end
         if @fc=4 begin
            update #tmpr set dis4=@dis1 where dis4='' and kategorie=@kat
         end
         if @fc=5 begin
            update #tmpr set dis5=@dis1 where dis5='' and kategorie=@kat
         end
         if @fc=6 begin
            update #tmpr set dis6=@dis1 where dis6='' and kategorie=@kat
         end

      fetch next from xc into @kat,@dis
    end
    close xc 
    deallocate xc

    update #TMPR set best= CONVERT(int, Pun1) + CONVERT(int, Pun2) + CONVERT(int, Pun3) + CONVERT(int, Pun4) + CONVERT(int, Pun5) + CONVERT(int, Pun6) 
    
    declare @rkatalt varchar(50)
    declare @rkat varchar(50)
    declare @rpk int
    declare @rang int
    declare @rang1 int
    declare @rbest int
    declare @rbest1 int
    set @rkatalt=''
    set @rang=0
    set @rang1=0
    set @rbest=0
    set @rbest1=0

    declare xx cursor for select pk, kategorie_kurzname, convert(int, best) from #tmpr order by kategorie_kurzname, convert(int, best) desc
    open xx
    fetch next from xx into @rpk, @rkat, @rbest
    while @@fetch_status = 0 begin
       if @rkatalt<>@rkat begin
          set @rkatalt=@rkat
          set @rang=0
       end   
       set @rang=@rang+1
       if @rbest <> @rbest1 begin
          set @rbest1=@rbest
          set @rang1=@rang
       end
       update #tmpr set rang=@rang1 where pk=@rpk
       fetch next from xx into @rpk, @rkat, @rbest
    end
    close xx
    deallocate xx
 
 
 declare @qw varchar(max)
  declare @rangliste varchar(max)

  declare xc cursor for
     select qw, rangliste from Disziplin_Mapping
  open xc
  fetch next from xc into @qw,@rangliste
  while @@FETCH_STATUS=0 begin
     update #tmpr set dis1=@rangliste where dis1=@qw
     update #tmpr set dis2=@rangliste where dis2=@qw
     update #tmpr set dis3=@rangliste where dis3=@qw
     update #tmpr set dis4=@rangliste where dis4=@qw
     update #tmpr set dis5=@rangliste where dis5=@qw

	 fetch next from xc into @qw,@rangliste
  end
  close xc
  deallocate xc 
    update #tmpr set pun4=0, lei4=0 where lei4=''   
    update #tmpr set pun3=0, lei3=0 where lei3=''  
    update #tmpr set pun2='0', lei2='0' where lei2=''  
    update #tmpr set pun1=0, lei1=0 where lei1=''    

    select * from #tmpr order by kategorie_kurzname, rang
    --select * into _mehrkampf from #tmpr
    drop table #tmpa
    drop table #tmpr

    return
ew:

print 'ew'
   insert into #tmpa SELECT dbo._tmpres.Rang, dbo._tmpres.name, dbo._tmpres.Jahrgang, dbo._tmpres.Lizenz, dbo._tmpres.Verein, dbo._tmpres.Kategorie_kurzname, dbo._tmpres.Kategorie, 
          dbo._tmpres.Disziplin, dbo._tmpres.F0, dbo._tmpres.F1, dbo._tmpres.F2, dbo._tmpres.F3, dbo._tmpres.F4, dbo._tmpres.F5, dbo._tmpres.F6, dbo._tmpres.F7, 
          dbo._tmpres.Best, dbo._tmpres.BestPunkte, dbo._tmpres.Land, dbo._tmpres.hochversuch, dbo._tmpres.hochbest, dbo._tmpres.team
   FROM   dbo.Res_Disziplin_Select INNER JOIN
          dbo._tmpres ON dbo.Res_Disziplin_Select.Disziplin = dbo._tmpres.Disziplin  AND dbo.Res_Disziplin_Select.Kategorie = dbo._tmpres.Kategorie_kurzname
   WHERE  (dbo.Res_Disziplin_Select.eg = 1) AND (dbo.Res_Disziplin_Select.OrtNr = @resortnr) 


   declare @jj int
   declare @pk int
   declare @sjj varchar(2)
   declare k cursor for 
      select pk, kategorie_kurzname, jahrgang from #tmpa
   open k
   fetch next from k into @pk, @kat, @jg
   while @@fetch_status=0 begin
      set @kat=substring(@kat,4,1)
      set @jj=year(getdate())-@jg

   if @jj<7 set @jj=7
     set @sjj=LTRIM(rtrim(str(@jj)))
      while LEN(@sjj) < 2 set @sjj='0'+@sjj
      if @kat='M' set @kat='M'+@sjj else set @kat='W'+@sjj


      update #tmpa set kategorie_kurzname=@kat where pk=@pk
      fetch next from k into @pk, @kat, @jg
   end
   close k
   deallocate k

   set @katalt=''
   set @disalt=''
   set @fc =0
   declare xc cursor for select distinct kategorie_kurzname, disziplin from #tmpa order by kategorie_kurzname, disziplin
   open xc
   fetch next from xc into @kat,@dis
   while @@fetch_status=0 begin
      if @kat<>@katalt begin
         set @fc=0
         set @katalt=@kat
      end
      if @dis<>@disalt
         set @fc=@fc+1
      declare xc1 cursor for select [name],[Jahrgang],[Lizenz],[Verein],[Kategorie_kurzname],[Kategorie],[Disziplin], best, bestpunkte, team
                              from #tmpa where kategorie_kurzname=@kat and disziplin=@dis
      open xc1
      fetch next from xc1 into @name, @jg, @lic, @verein, @kat_kurz, @kat, @dis1, @lei, @pun, @team
      while @@fetch_status = 0 begin
         set @cnt=(Select pk from #tmpr where name=@name and jahrgang=@jg and lizenz=@lic)
         if @cnt=0 or @cnt is null begin
            insert #tmpr ([name],[Jahrgang],[Lizenz],[Verein],[Kategorie_kurzname],[Kategorie],[Dis1],[Lei1],[Pun1],[Dis2],[Lei2],[Pun2],[Dis3],[Lei3],[Pun3],[Dis4],[Lei4],[Pun4],[Dis5],[Lei5],[Pun5],[Dis6],[Lei6],[Pun6],[Best],team)
            values (@name, @jg, @lic, @verein, @kat_kurz, @kat, '','','','','','','','','','','','','','','','','','',0,@team)
            set @cnt=(select top 1 pk from #tmpr order by pk desc)
         end
         if @fc=1 begin
            update #tmpr set lei1=@lei,dis1=@dis1, pun1=@pun where pk=@cnt
         end
         if @fc=2 begin
            update #tmpr set lei2=@lei,dis2=@dis1,  pun2=@pun where pk=@cnt
         end
         if @fc=3 begin
            update #tmpr set lei3=@lei,dis3=@dis1,  pun3=@pun where pk=@cnt
         end
         if @fc=4 begin
            update #tmpr set lei4=@lei,dis4=@dis1,  pun4=@pun where pk=@cnt
         end
         if @fc=5 begin
            update #tmpr set lei5=@lei,dis5=@dis1,  pun5=@pun where pk=@cnt
         end
         if @fc=6 begin
            update #tmpr set lei6=@lei,dis6=@dis1,  pun6=@pun where pk=@cnt
         end

         fetch next from xc1 into @name, @jg, @lic, @verein, @kat_kurz, @kat, @dis1,@lei, @pun,@team
      end
      close xc1
      deallocate xc1
         if @fc=1 begin
            update #tmpr set dis1=@dis1 where dis1='' and kategorie_kurzname=@kat_kurz
         end
         if @fc=2 begin
            update #tmpr set dis2=@dis1 where dis2='' and kategorie_kurzname=@kat_kurz
         end
         if @fc=3 begin
            update #tmpr set dis3=@dis1 where dis3='' and kategorie_kurzname=@kat_kurz
         end
         if @fc=4 begin
            update #tmpr set dis4=@dis1 where dis4='' and kategorie_kurzname=@kat_kurz
         end
         if @fc=5 begin
            update #tmpr set dis5=@dis1 where dis5='' and kategorie_kurzname=@kat_kurz
         end
         if @fc=6 begin
            update #tmpr set dis6=@dis1 where dis6='' and kategorie_kurzname=@kat_kurz
         end
      fetch next from xc into @kat,@dis
    end
    close xc 
    deallocate xc

    update #TMPR set best= CONVERT(int, Pun1) + CONVERT(int, Pun2) + CONVERT(int, Pun3) + CONVERT(int, Pun4) + CONVERT(int, Pun5) + CONVERT(int, Pun6) 
    set @rkatalt=''
    set @rang=0
    set @rang1=0
    set @rbest=0
    set @rbest1=0
    declare xx cursor for select pk, kategorie_kurzname, convert(int, best) from #tmpr order by kategorie_kurzname, convert(int, best) desc
    open xx
    fetch next from xx into @rpk, @rkat, @rbest
    while @@fetch_status = 0 begin
       if @rkatalt<>@rkat begin
          set @rkatalt=@rkat
          set @rang=0
       end   
       set @rang=@rang+1
       if @rbest <> @rbest1 begin
          set @rbest1=@rbest
          set @rang1=@rang
       end
       update #tmpr set rang=@rang1 where pk=@rpk
       fetch next from xx into @rpk, @rkat, @rbest
    end
    close xx
    deallocate xx

    update #tmpr set pun5=0, lei5=0 where lei5=''   
    update #tmpr set pun4=0, lei4=0 where lei4=''   
    update #tmpr set pun3=0, lei3=0 where lei3=''  
    update #tmpr set pun2='0', lei2='0' where lei2=''  
    update #tmpr set pun1=0, lei1=0 where lei1=''    

    declare @a varchar(50)
    declare @b varchar(50)
    declare @kat_kurzname varchar(50)
    declare xc cursor for select distinct kategorie_kurzname from #tmpr
    open XC
    fetch next from xc into @kat_kurzname
    while @@fetch_status=0 begin
       set @a=''
       select @a=dis1 from #tmpr where kategorie_kurzname=@kat_kurzname and dis1<>''order by dis1 desc
       print @kat_kurzname + ':' + @a
       if @a='' begin
          update #tmpr set dis1='', lei1='',pun1='' where kategorie_kurzname=@kat_kurzname
       end else begin
          update #tmpr set dis1=@a where kategorie_kurzname=@kat_kurzname
       end
       set @a=''
       select @a=dis2 from #tmpr where kategorie_kurzname=@kat_kurzname and dis2<>'' order by dis2 desc
       if @a='' begin
          update #tmpr set dis2='', lei2='',pun2='' where kategorie_kurzname=@kat_kurzname
       end else begin
          update #tmpr set dis2=@a where kategorie_kurzname=@kat_kurzname
       end
       set @a=''
       select @a=dis3 from #tmpr where kategorie_kurzname=@kat_kurzname and dis3<>'' order by dis3 desc
       if @a='' begin
          update #tmpr set dis3='', lei3='',pun3='' where kategorie_kurzname=@kat_kurzname
       end else begin
          update #tmpr set dis3=@a where kategorie_kurzname=@kat_kurzname
       end
       set @a=''
       select @a=dis4 from #tmpr where kategorie_kurzname=@kat_kurzname and dis4<>'' order by dis4 desc
       if @a='' begin
          update #tmpr set dis4='', lei4='',pun4='' where kategorie_kurzname=@kat_kurzname 
       end else begin
          update #tmpr set dis4=@a where kategorie_kurzname=@kat_kurzname
       end
       set @a=''
       select @a=dis5 from #tmpr where kategorie_kurzname=@kat_kurzname and dis5<>'' order by dis5 desc

       if @a='' begin
          update #tmpr set dis5='', lei5='',pun5='' where kategorie_kurzname=@kat_kurzname
       end else begin
          update #tmpr set dis5=@a where kategorie_kurzname=@kat_kurzname
       end
       fetch next from xc into @kat_kurzname
    end
    close xc
    deallocate xc
 
 
  declare xc cursor for
     select qw, rangliste from Disziplin_Mapping
  open xc
  fetch next from xc into @qw,@rangliste
  while @@FETCH_STATUS=0 begin
     update #tmpr set dis1=@rangliste where dis1=@qw
     update #tmpr set dis2=@rangliste where dis2=@qw
     update #tmpr set dis3=@rangliste where dis3=@qw
     update #tmpr set dis4=@rangliste where dis4=@qw
     update #tmpr set dis5=@rangliste where dis5=@qw

	 fetch next from xc into @qw,@rangliste
  end
  close xc
  deallocate xc 
    select *  from #tmpr order by kategorie_kurzname, rang
    drop table #tmpa
    drop table #tmpr

   
END




GO

/****** Object:  StoredProcedure [dbo].[res_set_disziplin_select]    Script Date: 26.05.2021 06:45:44 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO




-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[res_set_disziplin_select]
@ortnr int
as
insert dbo.res_disziplin_select

   (ortnr, kategorie, disziplin, qw, eg, bestenliste)
select distinct @ortnr as ortnr, kategorie_kurzname, disziplin, 0 as qw, 0 as eg, 0 as bestenliste
from dbo.res_resultate 
where ortnr=@ortnr




GO

