﻿DECLARE @TANEVRENDJE TABLE (
   C_AKTIV char(1)
  ,C_DATUM datetime
  ,C_HETIREND int
  ,C_HETNAPJA int
  ,C_MEGJEGYZES nvarchar(30)
  ,C_NAPTIPUSA int
  ,C_ORARENDINAP char(1)
  ,C_OSSZESCSOPORTRAVONATKOZIK char(1)
  ,C_CSENGETESIRENDID int
  ,TOROLT char(1)
  ,SERIAL int
  ,LASTCHANGED datetime
  ,CREATED datetime
  ,MODIFIER int
  ,CREATOR int
)

INSERT  @TANEVRENDJE (
  C_AKTIV, C_DATUM, C_HETIREND, C_HETNAPJA, C_MEGJEGYZES, C_NAPTIPUSA, C_ORARENDINAP, C_OSSZESCSOPORTRAVONATKOZIK, C_CSENGETESIRENDID, TOROLT, SERIAL, LASTCHANGED, CREATED, MODIFIER, CREATOR
) VALUES
   ('T', '20190902', 1554, 1408, N'', 1394, 'T', 'T', NULL, 'F', 0, GETDATE(), GETDATE(), NULL, NULL)
  ,('T', '20191023', 1554, 1412, N'', 1396, 'F', 'T', NULL, 'F', 0, GETDATE(), GETDATE(), NULL, NULL)
  ,('T', '20191028', 1554, 1412, N'', 1399, 'F', 'T', NULL, 'F', 0, GETDATE(), GETDATE(), NULL, NULL)
  ,('T', '20191029', 1554, 1412, N'', 1399, 'F', 'T', NULL, 'F', 0, GETDATE(), GETDATE(), NULL, NULL)
  ,('T', '20191030', 1554, 1412, N'', 1399, 'F', 'T', NULL, 'F', 0, GETDATE(), GETDATE(), NULL, NULL)
  ,('T', '20191031', 1554, 1412, N'', 1399, 'F', 'T', NULL, 'F', 0, GETDATE(), GETDATE(), NULL, NULL)
  ,('T', '20191101', 1554, 1412, N'', 1386, 'F', 'T', NULL, 'F', 0, GETDATE(), GETDATE(), NULL, NULL)
  ,('T', '20191223', 1554, 1412, N'', 1397, 'F', 'T', NULL, 'F', 0, GETDATE(), GETDATE(), NULL, NULL)
  ,('T', '20191230', 1554, 1412, N'', 1397, 'F', 'T', NULL, 'F', 0, GETDATE(), GETDATE(), NULL, NULL)
  ,('T', '20191231', 1554, 1412, N'', 1397, 'F', 'T', NULL, 'F', 0, GETDATE(), GETDATE(), NULL, NULL)
  ,('T', '20200102', 1554, 1412, N'', 1397, 'F', 'T', NULL, 'F', 0, GETDATE(), GETDATE(), NULL, NULL)
  ,('T', '20200103', 1554, 1412, N'', 1397, 'F', 'T', NULL, 'F', 0, GETDATE(), GETDATE(), NULL, NULL)
  ,('T', '20191224', 1554, 1412, N'', 6833, 'F', 'T', NULL, 'F', 0, GETDATE(), GETDATE(), NULL, NULL)
  ,('T', '20191225', 1554, 1412, N'', 1386, 'F', 'T', NULL, 'F', 0, GETDATE(), GETDATE(), NULL, NULL)
  ,('T', '20191226', 1554, 1412, N'', 1386, 'F', 'T', NULL, 'F', 0, GETDATE(), GETDATE(), NULL, NULL)
  ,('T', '20200101', 1554, 1412, N'', 1386, 'F', 'T', NULL, 'F', 0, GETDATE(), GETDATE(), NULL, NULL)
  ,('T', '20200124', 1554, 1412, N'', 1400, 'T', 'T', NULL, 'F', 0, GETDATE(), GETDATE(), NULL, NULL)
  ,('T', '20200315', 1554, 1412, N'', 1396, 'F', 'T', NULL, 'F', 0, GETDATE(), GETDATE(), NULL, NULL)
  ,('T', '20200409', 1554, 1412, N'', 1398, 'F', 'T', NULL, 'F', 0, GETDATE(), GETDATE(), NULL, NULL)
  ,('T', '20200414', 1554, 1412, N'', 1398, 'F', 'T', NULL, 'F', 0, GETDATE(), GETDATE(), NULL, NULL)
  ,('T', '20200410', 1554, 1412, N'', 1386, 'F', 'T', NULL, 'F', 0, GETDATE(), GETDATE(), NULL, NULL)
  ,('T', '20200413', 1554, 1412, N'', 1386, 'F', 'T', NULL, 'F', 0, GETDATE(), GETDATE(), NULL, NULL)
  ,('T', '20200501', 1554, 1412, N'', 1386, 'F', 'T', NULL, 'F', 0, GETDATE(), GETDATE(), NULL, NULL)
  ,('T', '20200430', 1554, 1411, N'', 1402, 'T', 'T', NULL, 'F', 0, GETDATE(), GETDATE(), NULL, NULL)
  ,('T', '20200601', 1554, 1412, N'', 1386, 'F', 'T', NULL, 'F', 0, GETDATE(), GETDATE(), NULL, NULL)
  ,('T', '20200615', 1554, 1408, N'', 1395, 'T', 'T', NULL, 'F', 0, GETDATE(), GETDATE(), NULL, NULL)

--SELECT * FROM @TANEVRENDJE

DECLARE
   @intezmenyId int
  ,@tanevId int

DECLARE tanevCursor CURSOR LOCAL FOR
SELECT ID, C_INTEZMENYID
FROM T_TANEV
WHERE C_NEV = '2019/2020'
  AND TOROLT = 'F'
  AND C_KOVETKEZO = 'T'

OPEN tanevCursor

FETCH NEXT FROM tanevCursor
INTO @tanevId, @intezmenyId

WHILE @@FETCH_STATUS = 0 BEGIN
  INSERT INTO T_TANEVRENDJE 
  (C_INTEZMENYID, C_TANEVID, C_AKTIV, C_DATUM, C_HETIREND, C_HETNAPJA, C_MEGJEGYZES, C_NAPTIPUSA, C_ORARENDINAP, C_OSSZESCSOPORTRAVONATKOZIK, C_CSENGETESIRENDID, TOROLT, SERIAL, LASTCHANGED, CREATED, MODIFIER, CREATOR)
  SELECT 
   @intezmenyId, @tanevId, C_AKTIV, C_DATUM, C_HETIREND, C_HETNAPJA, C_MEGJEGYZES, C_NAPTIPUSA, C_ORARENDINAP, C_OSSZESCSOPORTRAVONATKOZIK, C_CSENGETESIRENDID, TOROLT, SERIAL, LASTCHANGED, CREATED, MODIFIER, CREATOR
  FROM @TANEVRENDJE x
  WHERE NOT EXISTS (SELECT 1 FROM T_TANEVRENDJE tr WHERE x.C_DATUM = tr.C_DATUM AND tr.C_INTEZMENYID = @intezmenyId AND tr.C_TANEVID = @tanevId)

  IF @@ROWCOUNT > 0 BEGIN
    IF NOT EXISTS (SELECT 1 FROM T_NAPTARIHET WHERE C_TANEVID = @tanevId AND TOROLT = 'F') BEGIN
      EXEC dbo.sp_GenerateNaptariHetek @intezmenyId, @tanevId, 0
    END
    EXEC dbo.sp_GenerateNaptariNapok @intezmenyId, @tanevId, 0
  END

  FETCH NEXT FROM tanevCursor
  INTO @tanevId, @intezmenyId
END

CLOSE tanevCursor
DEALLOCATE tanevCursor
GO

