SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

IF OBJECT_ID('[dbo].[sp_GetOsztalyOrarendOsszes]') IS NOT NULL 
BEGIN
  DROP PROCEDURE [dbo].[sp_GetOsztalyOrarendOsszes]
END  
GO


CREATE PROCEDURE [dbo].[sp_GetOsztalyOrarendOsszes] 
  @tanevID int,
  @intezmenyId int
AS 
BEGIN
SET NOCOUNT ON;

SELECT DISTINCT ia.C_NEV as intezmeny, m.C_NEV as mukodesihely, d.C_NAME as oktatasiNevelesiFeladatTipus, 
  ISNULL(ia.C_VAROS + ', ', '') + FORMAT(GETDATE(), 'yyyy. MMMM dd.', 'hu-hu') AS kelt
FROM T_INTEZMENY_OSSZES i
  INNER JOIN T_INTEZMENYADATOK_OSSZES IA ON IA.C_INTEZMENYID=I.ID
  INNER JOIN T_MUKODESIHELY_OSSZES m ON IA.C_INTEZMENYID = m.C_INTEZMENYID AND m.TOROLT = 'F'
  INNER JOIN T_FELADATELLATASIHELY_OSSZES f ON m.ID = f.C_MUKODESIHELYID AND f.TOROLT = 'F'
  INNER JOIN (SELECT DISTINCT ID, C_NAME FROM T_DICTIONARYITEMBASE) d ON d.id = f.C_OKTATASINEVELESIFELADATTIPUS
  where i.id=@intezmenyId AND I.TOROLT='F'


declare @tabla table (osztalyId int, Ora int, Hetirend int, [1408] nvarchar(max), [1409] nvarchar(max), [1410] nvarchar(max), [1411] nvarchar(max), [1412] nvarchar(max), [1413] nvarchar(max), Hetirend_dname nvarchar(max),Osztalynev nvarchar(max))

DECLARE kurzor CURSOR FOR 
SELECT ocs.ID FROM T_OSZTALYCSOPORT_OSSZES ocs 
	inner join T_OSZTALY_OSSZES o on ocs.id=o.id
where ocs.TOROLT='F' and ocs.C_TANEVID=@tanevID and ocs.C_INTEZMENYID=@intezmenyId

DECLARE @OsztalyId int

OPEN kurzor
FETCH NEXT FROM kurzor INTO @OsztalyId

WHILE @@FETCH_STATUS = 0 BEGIN
  INSERT INTO @tabla
  EXEC sp_GetOrarendOsztalyonkent @intezmenyId, @tanevID, @osztalyid
  
      FETCH NEXT FROM kurzor INTO @OsztalyId
END

CLOSE kurzor
DEALLOCATE kurzor

SELECT * from @tabla

END

GO