DROP PROCEDURE IF EXISTS uspGetKIRAdattisztitas
GO
  CREATE PROCEDURE uspGetKIRAdattisztitas
    @pIntezmenyAzonosito NVARCHAR(30)
  AS
  BEGIN
    SET NOCOUNT ON;

    SELECT i.C_AZONOSITO
      ,t.C_NEV
      ,fh.C_OKTATASIAZONOSITO
      ,oa.OldOktAzon
      ,oa.NewOktAzon
      ,t4.OldElotag
      ,t4.NewElotag
      ,t4.OldVezeteknev
      ,t4.NewVezeteknev
      ,t4.OldUtonev
      ,t4.NewUtonev
      ,t4.OldSzuletesiNev
      ,t4.NewSzuletesiNev
      ,t4.OldAnyjaNeve
      ,t4.NewAnyjaNeve
      ,t4.OldSzuletesiDatum
      ,t4.NewSzuletesiDatum
      ,t4.OldSzuletesiHely
      ,t4.NewSzuletesiHely
    FROM T_FELHASZNALO_OSSZES fh
    INNER JOIN T_INTEZMENY_OSSZES i ON i.ID = fh.C_INTEZMENYID AND i.TOROLT = 'F'
    INNER JOIN T_TANEV_OSSZES t ON fh.C_TANEVID = t.ID AND t.TOROLT = 'F'
    LEFT JOIN dev.KIR_OH_Adattisztitas_OktAzon oa ON fh.ID = oa.Id
    LEFT JOIN dev.KIR_OH_Adattisztitas_NegyT t4 ON fh.ID = t4.Id
    WHERE i.C_AZONOSITO = @pIntezmenyAzonosito
      AND (
        oa.Id IS NOT NULL
        OR t4.Id IS NOT NULL
        )
      AND (
        t.C_NEV = '2017/2018'
        OR t.C_NEV = '2018/2019'
        )
      AND fh.TOROLT = 'F'
    ORDER BY t4.NewVezeteknev
  END
GO


