﻿--Migration
SET DATEFIRST 1

UPDATE tr SET
  C_HETNAPJA = hnJo.ID, LASTCHANGED = GETDATE(), MODIFIER = 0
FROM T_TANEVRENDJE tr 
  INNER JOIN T_DICTIONARYITEMBASE hn ON tr.C_HETNAPJA = hn.ID AND tr.C_TANEVID = hn.C_TANEVID
  INNER JOIN T_TANEV tv ON tv.ID = hn.C_TANEVID AND tv.C_NEV = '2021/2022'
  INNER  JOIN T_DICTIONARYITEMBASE hnJo ON DATEPART(dw, tr.C_DATUM) = hnJo.C_VALUE AND tr.C_TANEVID = hnJo.C_TANEVID AND hnJo.C_TYPE = 'HETNAPJATIPUS'
WHERE DATEPART(dw, C_DATUM) <> hn.C_VALUE
  AND tr.C_OSSZESCSOPORTRAVONATKOZIK = 'T'
  AND tr.C_DATUM IN (
     '20210901' ,'20211023' ,'20211025' ,'20211026'
    ,'20211027' ,'20211028' ,'20211029' ,'20211101'
    ,'20211222' ,'20211223' ,'20211224' ,'20211225'
    ,'20211226' ,'20211227' ,'20211228' ,'20211229'
    ,'20211230' ,'20211231' ,'20220101' ,'20220121'
    ,'20220314' ,'20220315' ,'20220414' ,'20220415'
    ,'20220417' ,'20220418' ,'20220419' ,'20220429'
    ,'20220501' ,'20220606' ,'20220615'
)
GO

/* declare variables */
DECLARE 
   @intezmenyId int
  ,@tanevId int

DECLARE IntCursor CURSOR FAST_FORWARD READ_ONLY FOR 
SELECT vi.C_INTEZMENYID, vi.C_TANEVID 
FROM V_INTEZMENYADATOK vi
WHERE vi.C_TANEVTIPUS = 'Köv'
  AND vi.C_FENNTARTOAZONOSITO < 999 --nen törölt

OPEN IntCursor

FETCH NEXT FROM IntCursor 
INTO @intezmenyId, @tanevId

WHILE @@FETCH_STATUS = 0
BEGIN
  EXEC sp_GenerateNaptariNapok
     @intezmenyId = @intezmenyId -- int
    ,@tanevId = @tanevId -- int
    ,@overrideDefault = 0 -- bit

  FETCH NEXT FROM IntCursor 
  INTO @intezmenyId, @tanevId
END

CLOSE IntCursor
DEALLOCATE IntCursor
GO

